json - What's wrong with my sed regex -
i have sed command replace json key-value pairs in format
"xxxxx" : "yyyyy" or
"xxxxx" : yyyyy here sed command,
sed -i 's/\("$\$xxxx\$\$\"\s*:\s*\"\)[^\"]*/\1yyyy/' now want modify above regex update key-value pairs.
example:
"xxxxx" : "yyyyy" should updated
"xxxxx" : "zzzzz" and
"xxxxx" : yyyyy should updated
"xxxxx" : zzzzz note difference, if value quotes, value should updated quotes , value doesn't have quotes then, should update also
this sed command came with, doesn't work.
sed -i 's/\("$\$"qc.testset.ids"\$\$\"\s*:\s*"*\)[^\"]*/\1123123"*/' anything missed here? how regex support both format?
use optional capture groups:
$ cat file "xxxxx" : "yyyyy" "xxxxx" : yyyyy "xxxxx" : "yyyyy", "xxxxx" : yyyyy, $ sed -r 's/^("xxxxx" : )(")?[^",]*(")?/\1\2zzzzz\3/' file "xxxxx" : "zzzzz" "xxxxx" : zzzzz "xxxxx" : "zzzzz", "xxxxx" : zzzzz,
Comments
Post a Comment