Which is the simple and fast UNIX command to print all lines from the last occurrence of a pattern?

awk, perl, sed, unix

Solution

This might work for you (GNU sed):

sed 'H;/pattern/h;$!d;x;//!d' file

Stashes the last pattern and following lines in the hold space and at end-of-file prints them out.

Or using the same method in awk:

awk '{x=x ORS $0};/pattern/{x=$0};END{if(x ~ //)print x}' file

However on my machine jaypals way with sed seems to be the quickest:

tac file | sed '/pattern/q' | tac

Problem

Which is the simple and fast UNIX command to print all lines from the last occurrence of a pattern to the end of the file ? ``` sed -n '/pattern/,$p' file ``` This sed command prints from the first occurrence onwards.

Original source