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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -