LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2005, 03:01 PM   #1
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
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 03:13 PM.
 
Old 05-28-2005, 03:43 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
How about
Code:
find . -exec grep -Hn hello {} \;
?
 
Old 05-28-2005, 04:35 PM   #3
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
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.
 
Old 05-28-2005, 04:53 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Are you sure there is no GNU version of grep available already (eg. with a name ggrep)?

I tested with the most archaic grep I could found
(the binary contains comment "SunOS 5.8 Generic February 2000") and
I got the command
Code:
find . -exec grep -n hello /dev/null {} \;
to do the same thing as my previous example in Linux systems.


Other solution is to compile working grep by yourself:
http://packages.debian.org/unstable/base/grep
 
Old 05-29-2005, 04:07 PM   #5
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
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.

Thanks a bunch!
 
Old 05-29-2005, 05:07 PM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
I know it's overkill, but this should also work

$ find / exec grepper.sh pattern {} \;
grepper.sh:
Code:
if grep "$1" "$2" &> /dev/null; then
    echo "$1"
    grep "$1" "$2"
add grep args to taste

hth --Jonas
 
Old 09-18-2006, 03:02 AM   #7
Sukhwinder Singh
LQ Newbie
 
Registered: Sep 2006
Posts: 4

Rep: Reputation: 0
Find the matching pattern and print it

My solution is:

#find / -print -exec grep 'hello' {} \;
 
Old 01-06-2008, 02:10 AM   #8
digitalDNA
LQ Newbie
 
Registered: Jan 2008
Posts: 1

Rep: Reputation: 0
Hello,

I use this form:

find . -exec grep -H -n 'hello' {} \;

-H -n display filename and line number


If you need another 'piped' grep, you can do this:

find . -exec grep -H -n 'hello' {} \; | grep 'line should contain this too'




Best Regards.
 
Old 12-18-2013, 05:36 PM   #9
boskyo
LQ Newbie
 
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
Using find & xargs w/ grep

I use find with xargs and it works well. Though it is a bit to type it can have further pipes attached at the end.

Leave off the -i and -n as desired.

Here is an example:

>> find . -name '*.[ch]' -print0 | xargs -0 -I file grep -inH 'gpio_request' file
./adi/adv7511_core-adi-fork-drm.c:859: ret = devm_gpio_request_one(&i2c->dev, adv7511->gpio_pd,
./via-camera.c:199: ret = gpio_request(cam->power_gpio, "viafb-camera");
./via-camera.c:204: ret = gpio_request(cam->reset_gpio, "viafb-camera");

Last edited by boskyo; 12-18-2013 at 05:37 PM.
 
Old 06-30-2016, 05:30 PM   #10
fkoff
LQ Newbie
 
Registered: Jun 2016
Posts: 1

Rep: Reputation: Disabled
grep

grep ‘^1[0-9][0-9][0-9]:’ /etc/passwd | cut –d ‘:’ –f1,3
date + "%A%d%B%y" display date
 
  


Reply

Tags
find, grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mplayer: Couldn't find matching filter/ao format! laurentwahl Linux - Software 2 07-12-2005 04:05 PM
error when rebooting could'nt find matching filesystem: label=/home bigdawg9950 Linux - Newbie 2 08-30-2004 02:03 AM
Couldn't find matching filesystem ashesh Linux - General 2 05-23-2004 01:18 AM
ut2003--- couldn't set video mode:couldn't find matching GLX visual <h1>tux</h1> Linux - Newbie 1 08-22-2003 07:17 PM
Couldn't find matching file system: LABEL=/user1 kausik Linux - General 4 05-20-2002 06:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration