简体   繁体   中英

sed command to regex in multiple lines and replace element in json file

I have a file like this:

test.json :

{..
}
  "Naveen": {
   "ip_addr": "INET;192.168.1.3;5001",
   "enable": 1,
   "mode": "emulate"
  },

I need to regex match Naveen and then the next element mode and change the mode value from emulate to xyz .

Failed solutions:

  1. sed '/^Naveen$/{$!{N;s/^Naveen\\nemulate$/xyz\\nBe/;ty;P;D;:y}}'
  2. cat test.json | tr '\\n' '\\r' | sed -e 's/Naveen\\remulate/xyz\\rBe/' | tr '\\r' '\\n'

What's the mistake I am making?

您可以尝试使用此sed

sed '/"Naveen": {/{:loop; N; s/emulate/CHANGED/g; /}/b; b loop;}' yourfile.json

This might work for you (GNU sed):

sed '/"Naveen": {/,/},/{/mode/s/emulate/xyz/}' file

Use a range between {...} and then focus on the specific string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM