LinuxQuestions.org
Help answer threads with 0 replies.
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, 01: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 01:54 AM.
 
Old 03-09-2011, 02:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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, 02:41 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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 02:44 AM.
 
Old 03-09-2011, 02: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11: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 11:31 PM
A backwards grep, perg perhaps? essdeeay Linux - General 11 10-06-2006 06:48 AM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 01:22 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 11:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration