使用sed或awk查找并替换JSON

时间:2021-03-03 19:32:06

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.

所以我需要能够使用sed或awk(最好在一行)为JSON注入一个值,我无法安装任何外部库来帮助我。

An example of the json is something like {"version":"0.5363"}

json的一个例子就像{“version”:“0.5363”}

I'd need to be able to inject a new value for version.

我需要能够为版本注入新值。

Any help would be greatly appreciated.

任何帮助将不胜感激。

1 个解决方案

#1


8  

You could try the below sed command,

您可以尝试以下sed命令,

$ 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命令中,将newvalue替换为您想要的值。添加内联编辑选项-i以保存所做的更改。

sed -i 's/regex/replacement/g' file

#1


8  

You could try the below sed command,

您可以尝试以下sed命令,

$ 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命令中,将newvalue替换为您想要的值。添加内联编辑选项-i以保存所做的更改。

sed -i 's/regex/replacement/g' file