0

I'm attempting to use sed to remove some lines from json files. As you can probably tell, I'm very much an amateur but think I have the regex correct. However sed throws various errors including unterminated address and unterminated 's' command.

The text I'm trying to remove is:

    {
  "trait_type": "Accessories",
  "value": "None"
}

And the regex I have to remove this (trait_type is different throughout different files and so I need to remove this entire block based on the value being "none")

(\{)([\r\n].+)(\r|\n|)(.*(?:None).*)([\r\n].*)

Using online regex testers, the above seems to work perfectly.

The command (and a couple of variations of) that I'm using is:

sed -e 's/\{([\r\n].+)(\r|\n|)(.*(?:None).*)([\r\n].*)//g' 2.json

Could anyone assist at all?

Many thanks

1 Answer 1

1

Assuming this is part of a JSON file, the following jq command would find all objects with a trait_type key and a value key and delete all of those objects where the value key has the value None.

jq 'del( .. |
    select(
      type == "object" and
      has("trait_type") and has("value") and 
      .value == "None"
    ) )' file

The command above would write the result of the operation to standard output. Redirect the output to a new file to save it.

1
  • Absolutely PERFECT. Thank you so much! Been here for hours trying to figure this out. Much appreciated
    – Danny P
    Sep 23, 2021 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .