![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| grep string and output filename | happyv | Shell Programming and Scripting | 3 | 11-20-2007 12:16 AM |
| Get Filename and Line Number using grep | ceemh3 | UNIX for Dummies Questions & Answers | 4 | 09-17-2007 10:35 AM |
| Grep Line with Matching Fields | hemangjani | UNIX for Advanced & Expert Users | 13 | 08-10-2007 11:46 AM |
| Grep all files matching partial filename | mharley | Shell Programming and Scripting | 3 | 06-08-2005 02:17 PM |
| Losing filename in grep output | netguy | Shell Programming and Scripting | 6 | 04-27-2004 11:39 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
How to grep / zgrep to output ONLY the matching filename and line number?
Hi all,
I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the whole matching line. I don't want to display the line content. Just the filename and linenumber needs to be displayed. I am also trying to play around with awk. Any response will be highly appreciated. Thanks in advance, vvaidyan |
|
||||
|
I would try something like this:
#!/usr/bin/ksh for i in `ls *.txt` do grep "<STRING>" $i 1>/dev/null 2>&1 if [[ $? = 0 ]] then echo -n "${i}: " grep -n "<STRING>" $i | awk -F: '{printf "%s",$1" "}' fi done echo |
|
||||
|
Thanks Keelba, from your program, I got it into one liner:
(echo "1.txt";echo "2.txt") | xargs zgrep -n STRING | awk -F: '{printf "%s %s", $1" -", $2 "\n"}' Later: xargs zgrep -n STRING | awk -F: '{printf "%s %s", $1" -", $2 "\n"}' I placed the above line in a file: vv-grep Then, I can run this script neatly as: (echo "1.txt";echo "2.txt") | ./vv-grep The final thing which I want to enhance here is only for the input string to be searched for, so that the program can be executed like: (echo "1.txt";echo "2.txt") | ./vv-grep STRING I am trying to find out how I can get input in command line parameter to the script. Thanks for the great help. I was able to understand your suggestion and could mould it in the way I wanted. It will be great if you could tell me if you know as how to get the input from comand line parameter to the script. vvaidyan |
|
||||
|
Got it...
accessed through $0 / $1 / $2, depending upon the position of the argument. Thanks, vvaidyan |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|