|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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: | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|