Reputation: 470
I would like to do append some characters to JSON values using sed.
For example given:
{"contactForm":{"title":"Mr","firstName":"Jimmy","phonenumber":"12341234"}}
I want to transform into:
{"contactForm":{"title":"_Mr_","firstName":"_Jimmy_","phonenumber":"_12341234_"}}
Note the values only are surrounded by underscores.
Using this expression almost results in the right matches (apart from matching curly braces at the end):
[^:]+(?=,|$)
See example at: https://regex101.com/r/nE5eV3/409
But I can't get sed to even print the capture as a starting point:
sed 's/[^:]+(?=,|$)/\\1/'
{"contactForm":{"title":"Mr","firstName":"Jimmy","phonenumber":"12341234"}}
{"contactForm":{"title":"Mr","firstName":"Jimmy","phonenumber":"12341234"}}
How would I use sed to surround these values with the underscore?
Upvotes: 1
Views: 70
Reputation: 626932
You may use
sed 's/:"\([^"]*\)"/:"_\1_"/g' file > newfile
The POSIX BRE pattern matches:
:"
- a :"
substring\([^"]*\)
- Capturing group 1: any 0 or more chars other than "
"
- a "
char.The RHS contains a \1
placeholder that pastes back the value stored in capturing group #1.
Upvotes: 1