Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-13-2012
Registered User
 
Join Date: Sep 2012
Posts: 10
Thanks: 11
Thanked 1 Time in 1 Post
Print file name when running grep from within find

Solaris 10

When running grep from within find command (don't know the technical term for 'running from within' ) , find command returns only the line which contains the pattern. Is there any way to get the file name printed as well ?


Code:
$ pwd
/opt/testdir/anotherDir
$
$
$ cat findme.txt
roses are red
$
$
$ cd ..
$
$ pwd
/opt/testdir
$
$
$ find ./ -name "findme*" 2> /dev/null
./anotherDir/findme.txt
$
#### ----------> Only the line which matches the pattern is retunred , not the file name
$
$ find ./ -name "findme*" -exec grep "roses*" {} \;
roses are red

Sponsored Links
    #2  
Old 11-13-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,285
Thanks: 151
Thanked 729 Times in 701 Posts
To print file name you can use -l option:-

Code:
find ./ -name "findme*" -exec grep -l "roses*" {} \;

The Following User Says Thank You to Yoda For This Useful Post:
omega3 (11-13-2012)
Sponsored Links
    #3  
Old 11-18-2012
sudon't's Avatar
Registered User
 
Join Date: May 2012
Location: The Cape Fear...ooooh!
Posts: 75
Thanks: 46
Thanked 0 Times in 0 Posts
Or you could use the -H flag with grep, which will return both the filename and the match. I believe the -l flag stops at the first match, which may or may not be what you want.

Last edited by sudon't; 11-18-2012 at 04:07 PM.. Reason: new thought
    #4  
Old 11-18-2012
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,466
Thanks: 62
Thanked 528 Times in 462 Posts
Quote:
Originally Posted by sudon't View Post
Or you could use the -H flag with grep, which will return both the filename and the match. I believe the -l flag stops at the first match, which may or may not be what you want.
The -H option is not in the standards and is not implemented on several UNIX and UNIX-like systems. (I don't think it is available on Solaris 10 systems.)

The -l option not only stops when it finds the first match, it only prints the name of the file containing the match (not the contents of the matching line).

A portable way to be sure the filename is printed is to be sure that at least two files are passed as operands to grep. For this case, especially if you have a lot of files, a better command line might be:

Code:
find . -name "findme*" -exec grep "roses" /dev/null {} +

which will call grep with several pathnames as long as {ARG_MAX} limits aren't exceeded. Adding /dev/null supplies a pathname that will never match any selected line and guarantees that at least two operands are given so grep will precede each matched line with the name of the file containing the matched line.
The Following 4 Users Say Thank You to Don Cragun For This Useful Post:
Corona688 (11-18-2012), elixir_sinari (11-18-2012), itkamaraj (11-19-2012), sudon't (11-19-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use grep & find command to find references to a particular file Gangam Shell Programming and Scripting 2 09-22-2011 03:52 AM
Find a string using grep & print the line above or below that. Zaib Shell Programming and Scripting 10 11-05-2010 08:51 AM
find file and print only contents with a hit by grep Timmää Shell Programming and Scripting 5 10-23-2009 01:35 AM
find file grep it and print file name borderblaster UNIX for Dummies Questions & Answers 4 03-03-2009 06:21 PM
need to grep or egrep the running processes in C file jimmynath UNIX for Advanced & Expert Users 5 09-08-2005 04:26 PM



All times are GMT -4. The time now is 06:22 AM.