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

How to delete first line of a file if it does not contain a specified string?

I have files that look like this:

username
Total 0
username
Total 0
username
Total 0
username
Total 0
username
Total 0

But some are malformed and have the incorrect first line like this:

Total 0
username
Total 0
username
Total 0
username
Total 0
username
Total 0

I need a one liner that will delete the first line if it does not contain username

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

i tried it with sed but it does not work:

sed -e '1!b' -e '/username/!d'

>Solution :

perl -ne 'print if 1 != $. || /username/'
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