Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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': None and the 3 sed commands have not worked (the first and third execute but print the file unchanged) and I am not sure if it is the space in the pattern or that 'id': can appear multiple times. Thank you :).

file

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

desired

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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}

sed

sed '/'id': None,/'d file
sed 's/'id':[[:space:]]None,/ file
sed 's/'id': None,//' file

>Solution :

You may use this sed:

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading