Get Filename and Line Number using grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Get Filename and Line Number using grep
# 1  
Old 09-11-2007
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 filename but grep -ln doesnt return the filename, the line number and the line of text.

Is it possible to do this using grep ?, or would I have to use something like awk ?

I would appreciate if anybody can help me

thanks

Last edited by ceemh3; 09-11-2007 at 11:22 AM..
# 2  
Old 09-11-2007
It's a bit of a kluge, but try
Code:
grep -nf test.txt /dev/null '{}' \;

# 3  
Old 09-11-2007
grep -l doesn't gives file name.it gives only the file name.So your code is true . Then what is wrong .

if use grep like these ;

grep -nf test.txt file1 file2 ...
grep -nf test.txt *
grep -nf test.txt *.java

it gives filename:linenumber:text

the problem is when you give file name to grep so it assumes that you know the file name so it doesnt write file name

when you use your code above when it finds a file it executes
grep -nf test.text filexx(known file)

you must find a way of using
grep -nf text.txt *.java
# 4  
Old 09-14-2007
you could do it something like this.

Firs create a script called, say mygrep, and put in it:

Code:
#!/bin/sh
grep -nf test.txt "$1" | sed -e "s!^!$1:!"

The you can do this:

Code:
find . -type f -name \*.java -exec ./mygrep {} \;

# 5  
Old 09-17-2007
Hi thanks a lot Kahuna, that works great
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

3. Shell Programming and Scripting

Grep line btn Number

HI Guys, I want grep Below line from a huge file. 01 01 -094.6 -093.3 -095.8 -094.0 01.3/01.3 18.1/18.7 I want to grep line which have data between -60 to -200 Thanks (4 Replies)
Discussion started by: pareshkp
4 Replies

4. Shell Programming and Scripting

Retrieving line number from grep

Hi. im trying to retrieve the line number from grep. i have 1 part of my code here. grep -n $tgt file.txt | cut -f 1 -d ":" when i do not cut the value i have is 12:aaa:abc:aaa:aaa:aaa how can i store the value of 12 or my whole line of string into a variable with grep? (6 Replies)
Discussion started by: One_2_three
6 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. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. Shell Programming and Scripting

Match String and get line number and filename

Hi All, I'm new to unix shell scripting.. Could someone guide me. I have to search a string in the entire directory, once the search string is matched, it should print the line number of the string that matches and also the line and along with it, it should print the file name. Thanks,... (5 Replies)
Discussion started by: thenz
5 Replies

9. UNIX for Dummies Questions & Answers

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... (3 Replies)
Discussion started by: vvaidyan
3 Replies

10. Shell Programming and Scripting

Grep a number from a line in ksh

In file.name, I have a line that reads $IDIR/imgen -usemonths -dropcheck -monitor -sizelimit 80000000 -interval 120 -volcal HSI How can I get the size limit, i.e. 80000000 out and pass it to a variable called SIZE? Thanks. I tried echo "grep sizelimit file.name" | sed -n -e... (3 Replies)
Discussion started by: rodluo
3 Replies
Login or Register to Ask a Question