How to grep / zgrep to output ONLY the matching filename and line number?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep / zgrep to output ONLY the matching filename and line number?
# 1  
Old 03-12-2008
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
# 2  
Old 03-12-2008
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
# 3  
Old 03-12-2008
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
# 4  
Old 03-12-2008
Got it...

accessed through $0 / $1 / $2, depending upon the position of the argument.

Thanks,
vvaidyan
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to control grep output intact for each matching line?

I have multiple (~80) files (some can be as big as 30GB of >1 billion of lines!) to grep on a pattern, and piped the match to a single file. I have a 96-core machine so that each grep job was sent to the background to speed up the search: file1.tab chr1A_part1 123241847 123241848... (6 Replies)
Discussion started by: yifangt
6 Replies

2. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Read each line in fileA, replace in all files with matching filename

Hello, I supposed that I asked the similar question but I could not have found it when I search on our forum. EXYU+: EXYU%3A+HRT+1 EXYU%3A+HRT+2 EXYU%3A+HRT+3 EXYU_url: #SERVICE 1:0:1:0:0:0:0:0:0:0:http%3A//xxxx%3Ayyyy@mmmm%3A55555/stream/channelname/YYYY?profile=htsp7777.ts #SERVICE... (2 Replies)
Discussion started by: baris35
2 Replies

4. UNIX for Dummies Questions & Answers

Using grep and zgrep then display the next few lines

Hello everyone. I would like to know if I can use grep or zgrep to search for a particular pattern then print the x number of lines after the pattern was found. Lets say for example a pattern was found on line 3, I wanted the output to show lines 3, 4 and 5. Thanks! (10 Replies)
Discussion started by: khestoi
10 Replies

5. UNIX for Dummies Questions & Answers

how to grep a number from output line

I`m having a output shown below, CFR 235,BBC DM-2 ALL CFR 111,BBC DM-2 ALL CFR 333,BBC DM-2 ALL from the above Output i want to use 235,111,333 as input for other purpose. these no always change every time i run script.so please suggest me the way i could do it with example,i have tried... (5 Replies)
Discussion started by: nitin_aaa27
5 Replies

6. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

7. Shell Programming and Scripting

grep string and output filename

Hello, I have over 200 files and some of them have the string like "John price $200". I would like to grep the string. Then output the filename which found the string. I have the following script, but it ONLY output the string echo Please input list file name: read listn for file in `cat... (3 Replies)
Discussion started by: happyv
3 Replies

8. UNIX for Dummies Questions & Answers

Get Filename and Line Number using grep

Hi, I am using the Korne shell to try and get the filename, line number and the line of text using grep e.g. find ./ -type f -name "*.java" -exec grep -nf test.txt '{}' \; (test.txt contains strings to search) will return the line number and the line of text. grep -l would return the... (4 Replies)
Discussion started by: ceemh3
4 Replies

9. Shell Programming and Scripting

Grep all files matching partial filename

What would be the easiest way to grep all files within a particular directory that match a partial filename? For example, searching all files that begin with "filename.txt" and are appended with the date they were created. I am using Ksh 88, btw. (3 Replies)
Discussion started by: mharley
3 Replies

10. Shell Programming and Scripting

Losing filename in grep output

I have the following line in a script that searches files in several directories and shows the search results on the screen. ls "$path" | xargs cat | tr -s " " | fgrep -i "$search_arg" But, because I'm also using CAT and TR, the output from the search does not display the name of the file... (6 Replies)
Discussion started by: netguy
6 Replies
Login or Register to Ask a Question