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

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

提交回复
热议问题