justaguy
justaguy

Reputation: 3022

removing pattern from lines in a file using sed or awk

I am trying to remove a pattern from all lines in a file. The pattern is 'id': null and the two sed attempts I have made execute but the file is unchanged.. Thank you :).

file

{
"objects": [
    {
        "version": "1a",
        "chromosome": "chr1",
                "id": null,
                "peptide": "123",
    },
    {
        "version": "1a",
        "chromosome": "chr1",
                "id": "This line has text and is printed.",
                "peptide": null,
    },
    {
        "version": '1a',
        "chromosome": "chr17",
                "id": null,
                "peptide": null}, 
                "id": 'This has text in it and this line is printed as well',
                "end": 460
            }
]
}

desired

{
"objects": [
    {
        "version": "1a",
        "chromosome": "chr1",
        "peptide": "123",
    },
    {
        "version": "1a",
        "chromosome": "chr1",
                "id": "This line has text and is printed.",
                "peptide": null,
    },
    {
        "version": '1a',
        "chrmosome": "chr17",
                "id": null,
                "peptide": null}, 
                "id": 'This has text in it and this line is printed as well',
                "end": 460
            }
]
}

sed

sed '/"id": *null/s/, [^,]*/ /' file --- if "id": null found substitute with blank up to the ending ,
sed -E "s/"id": *null, *//" file

Upvotes: 1

Views: 41

Answers (1)

anubhava
anubhava

Reputation: 786091

You may use this gnu-sed:

sed 0,'/"id": null,/{//d}' file

This will remove first instance of "id": null, from file


Original answer based on original question:

sed -E "s/'id': *None, *//" file

{'version': '1a', 'chr': '17', 'xref_json': None}, 'id': 'This has text in it and this line is printed', 'end': 460}
{'version': '1a', 'chr': '17', 'xref_json': None}

s/'id': *None, *// searches for pattern 'id':<0 or more spaces>None,<0 or more spaces> and replaces that with empty string.

Upvotes: 1

Related Questions