Find/grep command to find matching files, print filename, then print matching content
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Rep:
Find/grep command to find matching files, print filename, then print matching content
I'm looking for a find command which will do the following:
Find files that match some criteria
Grep for some text in those files
If grep finds matching text, print the filename, and then print the matching lines
Here's an example of what I am looking for:
./dir1/filename1
hello world
hello universe
./filename2
# Prints "hello penguin"
if [ $answer -eq "hello penguin" ]; then
./dir1/dir2/dir3/filename3
Post written by jellohellofellow on Aug 3, 2004
Seems simple, right? But I haven't found anything that does what I want.
Most people do this:
find . -type f |xargs grep hello
But I'm looking for a method that doesn't use xargs.
I see this alot:
find . -exec grep hello {} \; -print
But that command prints the results of 'grep hello' before it prints the filename, like this:
hello world
hello universe
./dir1/filename1
# Prints "hello penguin"
if [ $answer -eq "hello penguin" ]; then
./filename2
./dir1/dir2/dir3/filename3
Post written by jellohellofellow on Aug 3, 2004
./dir1/dir2/dir3/filename3
This is messy.
On most Unix distros, you can do this:
find . -exec grep -l hello {} \;
However, not all flavors of grep support the -l (--files-with-matches) flag. Also, this command only prints the matching files; it doesn't print the lines that match.
Last edited by stefanlasiewski; 05-28-2005 at 04:13 PM.
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
Thanks for the help, but I'm not sure if that will work. I'm working with a very primitive form of grep that doesn't support alot of the fancy features in GNU Grep.
I'll try it when I get back.
I'm working with Solaris 8 right now, but I'm also looking for a universal solution that works on AIX, HPUX, Alpha, and most flavors of Linux. GNU did it the right way. All the other vendors should follow GNU's lead.
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
That works great! Thanks.
Clever trick with the /dev/null . Without it, I don't get the filename, just the line numbers. With /dev/null, it looks like grep sees at least two files and knows to print the name of both files.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.