Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep from end of file

Tags:

linux

grep

unix

Is there a way to start the grep search from the end of the file instead of the beginning? Here is from the beginning --

$ grep -irn 'Qyt13_pUFjQ' ./

The information that I am looking for is close to the end of the file, but takes a few minutes to get all the way there from the start.

like image 583
David542 Avatar asked Mar 07 '15 22:03

David542


People also ask

How do you grep at the end of a line?

Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

How do I grep the last line of a file in Linux?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

How do I grep to a specific file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.

How do you reverse grep?

Add the -v option to your grep command to invert the results. Save this answer.


2 Answers

You can use tac to read the file line by line from the end to the beginning, then possibly switch it around again with another tac if you need to.

like image 134
PSkocik Avatar answered Oct 12 '22 05:10

PSkocik


You can do tail -n xxxx file.txt | grep ... to pipe the last xxxx lines of the file through grep.

like image 34
mti2935 Avatar answered Oct 12 '22 04:10

mti2935