Home > Back-end >  about pratice linux sed
about pratice linux sed

Time:11-21

"caprometheusip" : {
      "id" : 11,
      "key" : "caprometheusip",
      "value" : "[fd02::100:ffff:ffff:ffff:e0]",
      "description" : "",
      "createTime" : 1660630139000,
      "updateTime" : 1644822836000
    },

There are the following fields in my json file. I want to replace this IP address, which corresponds to value, with ""

I tried to pass this command

Sed - i - e 's/ "[fd02:: 100: ffff: ffff: ffff: e0] "/ ""/' 1.json

After json executes, it has no effect

I wonder if it is because the Ip address starts with [. The sed command resolves to regular. If so, how should I modify the command

CodePudding user response:

Brackets are special characters and need to be escaped with \. Try this:

sed -i -e 's/\[fd02::100:ffff:ffff:ffff:e0\]//g' 1.json

Note that I also removed the extra spaces from the ip address in your expression and added the g flag in the substitue command in order to make sed replace all occurrences of the matched pattern.

CodePudding user response:

You need to escape the brackets as [] are special characters

Using sed

$ sed -i.bak '/value/s/\[fd02::100:ffff:ffff:ffff:e0]//'
"caprometheusip" : {
      "id" : 11,
      "key" : "caprometheusip",
      "value" : "",
      "description" : "",
      "createTime" : 1660630139000,
      "updateTime" : 1644822836000
    },
  • Related