Grep's line numbers parsed into one line of output.
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.