Home > Blockchain >  How to replace a matching string with empty string in a JSON file?
How to replace a matching string with empty string in a JSON file?

Time:10-12

I have a file where each line is a JSON object:

{"index":{}}
{"msg_text":"yes","@timestamp":"2019-03-31T00:01:59.574Z","o_error":"INTERNAL_INVALID_MESSAGE","sender":"353830465174","account_id":"e15965cf-ffc1-40ae-94c4-b450ab190222","recipient":"49223","@version":1,"submission_ts":1553990519574000,"delivery_ts":1553990519574000}
{"index":{}}
{"msg_text":"yes","@timestamp":"2019-03-31T00:01:59.574Z","o_error":"INTERNAL_INVALID_MESSAGE","sender":"353830478555","account_id":"e15965cf-ffc1-40ae-94c4-b450ab190222","recipient":"49223","@version":1,"submission_ts":1553990519574000,"delivery_ts":1553990519575000}
{"index":{}}

I want to remove all the delivery_ts fields and values. So the following is what I want:

{"index":{}}
{"msg_text":"yes","@timestamp":"2019-03-31T00:01:59.574Z","o_error":"INTERNAL_INVALID_MESSAGE","sender":"353830465174","account_id":"e15965cf-ffc1-40ae-94c4-b450ab190222","recipient":"49223","@version":1,"submission_ts":1553990519574000}
{"index":{}}
{"msg_text":"yes","@timestamp":"2019-03-31T00:01:59.574Z","o_error":"INTERNAL_INVALID_MESSAGE","sender":"353830478555","account_id":"e15965cf-ffc1-40ae-94c4-b450ab190222","recipient":"49223","@version":1,"submission_ts":1553990519574000}
{"index":{}}

How can I do this using Linux commands?

CodePudding user response:

May be you need to use sed command

As per your output it shows you are replacing delivery_ts to submission_ts

sed -i 's/delivery_ts/submission_ts/g' file.json

If you wan to replace delivery_ts to empty then use below command

sed -i 's/delivery_ts//g' file.json

CodePudding user response:

Editing JSON is what jq exists to do.

jq -c 'del(.delivery_ts)' <in.json >out.json

See this running against your stated input at https://jqplay.org/s/v1-J5cTMA-

  • Related