Bash : Remove everthing before special caracter

Viewed 47

I got a big json file with text to be modify in each value. If we look a the first entry, it looks like :

$ cat description6fr.json | jq '.[0].fr'
Something\nSomethings\n\nSomething\n\nSomething

How to cut everything before the first \n\n ? I did try many things with sed and awk, but help is welcome. Thanks!

1 Answers

You could use index to find the match and then slice it:

jq '.[0].fr | .[index("\n\n") + 2:]' description6fr.json

If the string does not match \n\n, this will truncate the first two characters, so you might prefer something like:

jq '.[0].fr| if index("\n\n") then .[index("\n\n") + 2:] else . end' description6fr.json

you can parameterize that with:

jq --argjson s '"\n\n"' 'if index($s) then .[index($s) + 2:] else . end'
Related