sed 替换 json 对象中键的值

mco*_*lin 5 regex json replace sed

我想做什么?

给定一个 json 事件文件。我想通过关键字定位特定事件,然后将该事件中的键值替换为“”。这必须使用 sed 来完成(Splunk 转发问题..我不会用细节来烦你)。

事件示例

{
  "message":"we have a response from SomeService",
  "other":"some stuff",
  "other2":"some other stuff",
  "xml":"<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:awsse=\"http://xml.chicken.com/2010/06/Session_v3\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"><Header><To>http://www.w3.org/2005/08/addressing/anonymous</To><From><Address>..... AND SO ON BIG GIANT NASTEY XML",
  "other3":"even more stuff"
}
Run Code Online (Sandbox Code Playgroud)

期望的结果

{
  "message":"we have a response from SomeService",
  "other":"some stuff",
  "other2":"some other stuff",
  "xml":"",
  "other3":"even more stuff"
}
Run Code Online (Sandbox Code Playgroud)

我尝试了什么? 我可以隔离事件并更换钥匙,没有问题。我正在努力使用正则表达式来替换json 中键的值。

cat test.json | sed '/"we have a response from SomeService"/ s/other2/chicken/'
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

pau*_*aul 6

复制评论

你可以尝试这个

cat test.json | sed '/"xml":/ s/"xml":[^,]*/"xml":""/'
Run Code Online (Sandbox Code Playgroud)

[^,]*将匹配所有内容,直到,找到为止。