Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 18th, 2004, 9:52 AM   #1
cfriend
Newbie
 
Join Date: Jul 2004
Posts: 8
Rep Power: 0 cfriend is on a distinguished road
Hi

Is there any way with grep to show me only the last pattern that matches and not all ? For example for a file with

linux 56
slackware 32
debian 34
linux 2
redhat 9

and $> grep linux filename

show me only --> linux 2
and not

linux 56
linux 2

Thanks
cfriend is offline   Reply With Quote
Old Sep 18th, 2004, 10:19 AM   #2
Ashcroft
Programmer
 
Join Date: Sep 2004
Posts: 38
Rep Power: 0 Ashcroft is on a distinguished road
Hmm, bit tricky - you could do it with awk (and maybe sed) - but I'm not sure how to do it with grep.

Here is a perl one liner that will do it though.

$ perl -ne 'next unless (/linux/); $v = $_; { END { print $v } }' foo.txt

If you really have to do it with just shell tools just use awk to do pretty much the same thing that perl one liner does, overwrite a variable with each line that matches the pattern and have an END statement print the variable.
Ashcroft is offline   Reply With Quote
Old Sep 18th, 2004, 8:33 PM   #3
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
Nice oneliner, bro. Overkill, but very nice... I could crank out an awk version, but it would be too similiar to that one, and it just does the job well enough. If you want it to run a little bit faster, try this:
$ grep "linux" foo | tail -1
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Sep 19th, 2004, 5:54 AM   #4
Ashcroft
Programmer
 
Join Date: Sep 2004
Posts: 38
Rep Power: 0 Ashcroft is on a distinguished road
Doh! Yeah 'tail' is a much simpler and faster way to do it.

-but you can never have too much overkill
Ashcroft is offline   Reply With Quote
Old Sep 19th, 2004, 6:20 PM   #5
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
I really thought the awk solution to this would look the same or atleast very similiar. Little did I know that because of the lack of $_ in awk, I had to do a little research. This is a little bit more light-weight than the perl version above, but since perl is the all-in-one swiss army chainsaw, they go hand-in-hand depending on what you do:
$ awk '/linux/ { last = $0 }; END { print last }'foo
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC