LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-09-2011, 12:51 AM   #1
Hisu
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Rep: Reputation: 0
Backwards double grep?


I have a file like this:
Code:
<unknown random non-numberic garbage> 7893659783
<unknown number of garbage lines here>
<unknown random non-numberic garbage> notable line
<unknown number of garbage lines here>
<unknown random non-numberic garbage> 531269753
<unknown number of garbage lines here>
<unknown random non-numberic garbage> 35087513
<unknown number of garbage lines here>
<unknown random non-numberic garbage> notable line
I need to search it backwards for "notable line" strings AND the first numeric string preceding that line. I've discovered that it's easy to search backwards by tac file | grep pattern, but how to tell grep to continue searching from where the pattern was found? In short, I want to make the output of the example above into this:

Code:
<unknown random non-numberic garbage> notable line
<unknown random non-numberic garbage> 35087513
<unknown random non-numberic garbage> notable line
<unknown random non-numberic garbage> 7893659783
Or I must use another tool, like awk or sed? If so, how do I do so?

Last edited by Hisu; 03-09-2011 at 12:54 AM.
 
Old 03-09-2011, 01:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure grep would have this ability, but awk would be easy enough:
Code:
tac file | awk '/notable line$/{x=1;print}x && /[0-9]+$/{x=0;print}'
 
Old 03-09-2011, 01:41 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Without having a little more detail on how you identify these lines, I can't give a workable solution. In fact, I can't be certain it's possible, but you could probably do it by cascading grep.

Assuming regex1 matches all lines like so:
Code:
<unknown random non-numberic garbage> <some number>
And assuming regex2 matches all lines like so:
Code:
<unknown random non-numberic garbage> notable line
Then something like this might work:
Code:
tac file | egrep "regex1\|regex2" | egrep -A 1 "regex2"
The tac reverses the file like you want.
The first egrep matches both types of lines.
The second egrep prints one line of context after the "notable lines." That first line of context should be the first numeric line like you want.

That is... unless I misunderstood the problem.

EDIT:
Keep in mind, depending on how regex1 and regex2 are written, they might need to be surrounded by parentheses to make sure the OR'ing in the first egrep behaves like you want. Again, with some more detail and a sample data file, I might be able to give a working command.

Last edited by Dark_Helmet; 03-09-2011 at 01:44 AM.
 
Old 03-09-2011, 01:44 AM   #4
Hisu
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Works like magic, just as I want it to. Thank you!
 
  


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
bash script grep without double saavik Programming 2 05-14-2009 10:15 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
A backwards grep, perg perhaps? essdeeay Linux - General 11 10-06-2006 05:48 AM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:42 AM.

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