I have to replace every string after colon with the same word + underscore + number in following way:
{"first": "1_first", "second": "1_second"} Expected results:
{"first": "first_1", "second": "second_1"} {"first": "first_20", "second": "second_20"} {"first": "first_33", "second": "second_33"} I've succeeded with the first one:
echo '{"first": "first_1", "second": "second_1"}' | sed "s/\( \".*\",\)/ \"first_$j\",/" Result is:
{"first": "first_888", "second": "second_1"} But have problems with the second one. I suppose that this expression is too greedy:
echo '{"first": "first_1", "second": "second_1"}'|sed "s/\( \".*\)\"}/ \"second_$j\"}/" This one cuts too much:
{"first": "second_888"} Maybe there is some more elegant way to do this? With one expression instead of 2?
"key": "value", do you mean the replacement word should be the key or the value?jqorjsonpipeor some other json parser for modifying or extracting data from json. regexps don't work reliably for json for the same reason that they don't for xml or html: Don't parse XML or HTML with regular expressions. It doesn't work.