![]() |
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 |
| Using grep to extract line number | mskarica | Shell Programming and Scripting | 8 | 06-26-2008 02:47 AM |
| How to grep / zgrep to output ONLY the matching filename and line number? | vvaidyan | UNIX for Dummies Questions & Answers | 3 | 03-12-2008 05:33 PM |
| Grep a number from a line in ksh | rodluo | Shell Programming and Scripting | 3 | 12-09-2007 09:31 AM |
| grep the string with the line number | salaathi | Shell Programming and Scripting | 4 | 12-04-2007 11:42 AM |
| is there any why to get the number of line in grep result ? | umen | UNIX for Dummies Questions & Answers | 1 | 03-16-2006 09:56 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 09:22 AM.. |
|
||||
|
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 |
|
||||
|
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:!" Code:
find . -type f -name \*.java -exec ./mygrep {} \;
|
![]() |
| Bookmarks |
| Tags |
| grep or |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|