How to search for a pattern from bottom to top.

Hi,
I have a file, which is having a pattern "SEARCH" somewhere towards end of the file,

if i am giving " grep -i "SEARCH" $File" , it is taking too much time as file is very big.

So i want to search for the pattern from the back side of the file, how can we search for a pattern in bottom to top of a file.

Thanks

You can use the following commands (print the first occurence of SEARCH starting from bottom to top of file) :

tac inputfile | sed -n '/SEARCH/{p;q;}'

On my AIX boc the tac command isn't avalaible, so i use the tail command (which can't display more than 20K) :

cat -r inputfile | sed -n '/SEARCH/{p;q;}'
> ls -l bigfile
-rw-r--r-- 1 at90367 mkgroup-l-d 264904744 Aug  8 10:44 bigfile
> tail -2 bigfile
===> pattern <===
===> End Of File <===
> time grep '=> pattern <=' bigfile | tail -1
===> pattern <===

real    1m42.796s
user    0m0.249s
sys     0m0.453s
> time tac bigfile | sed -n '/=> pattern <=/{p;q;}'===> pattern <===

real    0m3.360s
user    0m0.060s
sys     0m0.030s
>

Jean-Pierre

cat -r is giving me

cat: illegal option -- r

See Need to read a file in reverse

Jean-Pierre

Thank you so much, :slight_smile:

if you do not have tac or -r option from cat, you can just use tail. make a loop and keep tailing and grepping until you find the search pattern.