Find and replace JSON with sed or awk

Therefore, I need to be able to enter a value in JSON using sed or awk (preferably on a single line), and I cannot install any external libraries to help me.

The json example is something like {"version":"0.5363"}

I will need to add a new value for the version.

Any help would be greatly appreciated.

+6
source share
1 answer

You can try the following sed command,

 $ echo '{"version":"0.5363"}' | sed 's/\({"version":"\)[^"]*\("}\)/\1newvalue\2/g' {"version":"newvalue"} 

In the sed command above, replace newvalue with the desired value. Add the -i built-in edit option to save your changes.

 sed -i 's/regex/replacement/g' file 
+7
source

Source: https://habr.com/ru/post/976497/


All Articles