Reputation: 316
So I need to be able to inject a value into JSON using sed or awk (preferably on one line) and I cannot install any external libraries to help me.
An example of the json is something like {"version":"0.5363"}
I'd need to be able to inject a new value for version.
Any help would be greatly appreciated.
Upvotes: 11
Views: 12409
Reputation: 6171
We can use \s*
to match any whitespace between fields, which is irrelevant in JSON:
$ echo '{ "version": "0.5363" }' | sed 's/\s*\("version"\s*:[^"]*\)\s*\"[^"]*\"/\1\"newvalue\"/g'
{ "version": "newvalue" }
Upvotes: 0
Reputation: 174696
You could try the below sed command,
$ echo '{"version":"0.5363"}' | sed 's/\({"version":"\)[^"]*\("}\)/\1newvalue\2/g'
{"version":"newvalue"}
In the above sed command, replace the newvalue
with value you want. Add inline edit option -i
to save the changes made.
sed -i 's/regex/replacement/g' file
Upvotes: 9