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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-13-2006, 07:39 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2006
Location: Australia
Distribution: Fedora Core 4 and Cygwin
Posts: 14
Rep:
|
Grep's line numbers parsed into one line of output.
Code:
echo `grep -n foo bar.txt | cut -d: -f1` | tr ' ' :
The -n option to grep prefixes each match with a corresponding line number. The cut gets rid of the matches, giving a list of numbers. The echo converts the newlines into spaces, except for the last newline which is removed. Finally tr is used to convert spaces into colons.
If bar.txt contains foo on lines 15, 16, 21 and 26, the final output will be 15:16:21:26.
Can anyone show me a simpler way of doing this?
Thanks in advance.
|
|
|
|
08-13-2006, 08:59 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
If a : at the end of a line is no problem, you can do this with one command:
awk '/foo/ { printf FNR ":" } END { print"" }' bar.txt
Your output will look like this:
15:16:21:26:
Don't know if you have any experience with AWK, so here's a little breakdown:
/foo/ => looks for lines with foo in them, if that is the case:
printf FNR ":" => print the linenumber (FNR) followed by a : (no newline, that's why printf is used instead of print)
The print"" in the END section prints a newline.
But, like I stated before, there will be an extra : at the end.
Hope this helps.
|
|
|
|
08-13-2006, 10:01 AM
|
#3
|
|
LQ Newbie
Registered: Jan 2006
Location: Australia
Distribution: Fedora Core 4 and Cygwin
Posts: 14
Original Poster
Rep:
|
Quote:
|
Originally Posted by druuna
Hi,
If a : at the end of a line is no problem, you can do this with one command:
awk '/foo/ { printf FNR ":" } END { print"" }' bar.txt
Your output will look like this:
15:16:21:26:
Don't know if you have any experience with AWK, so here's a little breakdown:
/foo/ => looks for lines with foo in them, if that is the case:
printf FNR ":" => print the linenumber (FNR) followed by a : (no newline, that's why printf is used instead of print)
The print"" in the END section prints a newline.
But, like I stated before, there will be an extra : at the end.
Hope this helps.
|
I like that method. I can get rid of the last colon using:
Code:
awk '/foo/ { printf ":" FNR }' bar.txt | cut -c2-
Thanks for your help.
|
|
|
|
08-13-2006, 10:19 AM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
Yes, getting rid of the last colon isn't hard, but do you really need to?
Starting a shell is one of the more resource intensive actions, when piping something to something else (awk ... | cut ...) you need a shell for all the commands (2 in this example. The first example you gave needs 3 shells) and pipes between them have to be set up.
It sometimes is wiser to leave the output the way it is, depending on what you are going to do next.
|
|
|
|
08-13-2006, 12:54 PM
|
#5
|
|
Member
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163
Rep:
|
awk '/foo/{print FNR ":"}' will result in:
15:
16:
21:
26:
...
to solve all the problems:
Code:
awk 'BEGIN{ORS="";first=1}/foo/{if (first==1){first=0;print FNR}else{print ":" FNR}}END{print "\n"}' bar.txt
If you want to search for just the word foo, and not food or afoo, you need to use
Code:
awk 'BEGIN{ORS="";first=1}
/([^[:alnum]]|^)foo([^[:alnum:]]|$)/
{if (first==1){first=0;print FNR}else{print ":" FNR}}
END{print"\n"}' bar.txt
and to match foo case-insensitivitely, replace foo in the RE (between the slashes) with [Ff][Oo][Oo]
Last edited by burninGpi; 08-13-2006 at 02:32 PM.
|
|
|
|
08-13-2006, 06:34 PM
|
#6
|
|
LQ Newbie
Registered: Jan 2006
Location: Australia
Distribution: Fedora Core 4 and Cygwin
Posts: 14
Original Poster
Rep:
|
Thank you Burnin. Those commands do everything I need.
Druuna, I'm not sure if I really need to remove the last colon.
Last edited by judgex; 08-13-2006 at 06:36 PM.
|
|
|
|
08-14-2006, 03:54 AM
|
#7
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
burninGpi already did the extra awk work, go for that example. It's complete, resource friendly and basically elegant 
|
|
|
|
08-14-2006, 04:10 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
Code:
grep -n foo bar.txt| cut -f1 -d: | xargs
Code:
perl -ne 'print "$. " if /foo/' bar.txt
|
|
|
|
08-14-2006, 04:22 AM
|
#9
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
The perl code snippet has the same pitfall as my command: It will print a charachter (a space in your perl example) after the last found entry.
burninGpi's example still seems to be the only one-liner that works without extra pipe(s).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:21 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|