The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-11-2007
ceemh3 ceemh3 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
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..
  #2 (permalink)  
Old 09-11-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
It's a bit of a kluge, but try
Code:
grep -nf test.txt /dev/null '{}' \;
  #3 (permalink)  
Old 09-11-2007
fazliturk fazliturk is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 45
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 (permalink)  
Old 09-17-2007
ceemh3 ceemh3 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
Hi thanks a lot Kahuna, that works great
  #5 (permalink)  
Old 09-14-2007
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
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 {} \;
Closed Thread

Bookmarks

Tags
grep or

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0