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

前端 未结 3 937
余生分开走
余生分开走 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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-21 20:43

    Since this is file-dependent and not line-dependent, awk can be a better tool.

    Just keep a counter on how many times this happened:

    awk -v patt="Header" '$0 == patt && ++f==2 {next} 1' file
    

    This skips the line that matches exactly the given pattern and does it for the second time. On the rest of lines, it prints normally.

    0 讨论(0)
提交回复
热议问题