Print file name when running grep from within find | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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

Print file name when running grep from within find

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-13-2012
omega3 omega3 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 4:56 AM EDT
Posts: 29
Thanks: 29
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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 2 September 2014, 9:11 PM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 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
sudon't sudon't is offline
Registered User
 
Join Date: May 2012
Last Activity: 25 June 2013, 1:52 PM EDT
Location: The Cape Fear...ooooh!
Posts: 80
Thanks: 50
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
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 2 September 2014, 11:40 PM EDT
Location: San Jose, CA, USA
Posts: 4,506
Thanks: 177
Thanked 1,512 Times in 1,284 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 03:00 AM.