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

前端 未结 3 985
余生分开走
余生分开走 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:41

    This might work for you (GNU sed):

    sed -i '1b;/^Header/d' file
    

    Ignore the first line and then remove any occurrence of a line beginning with Header.

    To remove subsequent occurrences of the first line regardless of the string, use:

    sed -ri '1h;1b;G;/^(.*)\n\1$/!P;d' file
    

提交回复
热议问题