LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2015, 02:07 PM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
Grep and catch the last result


I have huge files and i want to grep only the last line that grep find.

I don't want to use tail since count line is change also from file to file and I don't want grep will search the all file.

for example file contain:

Quote:
... (1000000 lines)
bla bla bla
la la la
OPS hello
--- (5000000 lines)
No yes no
kuku OPS kuku
opala OPS
... (90000 lines)
grep to find OPS will give the last result
"opala OPS"

I don't want grep will work the all file since it can be Giga or more.
only search from the end of the file until he catch the "OPS" word.

Thanks
 
Old 06-30-2015, 02:17 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You could use tac (think, "cat" spelled backwards) to list the lines in reverse order and then print the first match. Something like
Code:
tac somefile | grep -m 1 OPS
It's still going to be a bit slow. Reading a file backward is not as efficient as reading in the forward direction (unless someone has built "backward readahead" into the kernel while I wasn't looking).

Last edited by rknichols; 06-30-2015 at 02:20 PM. Reason: Delete extraneous quote mark
 
1 members found this post helpful.
Old 06-30-2015, 02:19 PM   #3
netnix99
Member
 
Registered: Jun 2011
Distribution: redhat, CentOS, OpenBSD
Posts: 298

Rep: Reputation: 99
This may not be exactly what you are looking for, but it does work...
Code:
tac filename | grep -m 1 "OPS"
tac is the opposite of cat, and it starts at the end of the file. grep -m 1 "target" searches for the target and stops after the first match his found.
 
1 members found this post helpful.
Old 06-30-2015, 02:24 PM   #4
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Catch result of extract file of 7z gajananh999 Linux - Software 9 10-26-2012 02:38 AM
output result from grep. brian00 Linux - Newbie 1 09-12-2012 09:39 AM
Catch errors with grep Aerosilver Programming 9 08-31-2012 04:35 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 05:28 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration