Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

sed

awk

perl

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.

like image 553
Sidharth C. Nadhan Avatar asked Jul 11 '14 05:07

Sidharth C. Nadhan


1 Answers

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
like image 69
potong Avatar answered Oct 12 '22 08:10

potong