bashunixsed

How to delete all lines before the second match of a pattern with sed?


I have the following file:

first
second
third
fourth
third
fifth
sixth

Using cat file | sed -n '/third/,$p' I can print starting from the first match, in order to get:

third
fourth
third
fifth
sixth

Is it possible to modify the sed command such that it essentially ignores the first match and prints from the second match? That would be:

third
fifth
sixth

Solution

  • With sed:

    sed '1,/third/d' file | sed -n '/third/,$p'
    

    Output:

    third
    fifth
    sixth