Search code examples
perlawksed

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

i tried it with sed but it does not work:

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

Solution

  • perl -ne 'print if 1 != $. || /username/'