Sed/Awk to delete second occurence of string - platform independent

前端 未结 3 940
余生分开走
余生分开走 2021-01-21 19:55

I\'m looking for a line in bash that would work on both linux as well as OS X to remove the second line containing the desired string:

Header
1
2
...
Header
10
1         


        
3条回答
  •  情话喂你
    2021-01-21 20:22

    I would recommend using awk for this:

    awk '!/^Header/ || !f++' file
    

    This prints all lines that don't start with "Header". Short-circuit evaluation means that if the left hand side of the || is true, the right hand side isn't evaluated. If the line does start with Header, the second part !f++ is only true once.

    $ cat file
    baseball
    Header and some other stuff
    aardvark
    Header for the second time and some other stuff
    orange
    $ awk '!/^Header/ || !f++' file
    baseball
    Header and some other stuff
    aardvark
    orange
    

提交回复
热议问题