embedded grep: how to get line and file name?

embedded grep: how to get line and file name?

Post by Trevor Bac » Thu, 26 Nov 1998 04:00:00



Running this command:

grep "typedef.*pid_t" *

Produces output like this:

types.h:typedef short        pid_t;        /* used for process ids */

(specifically, both the filename and matched line are displayed).

However, when I embed essentially the same grep command as part of find like
this:

find /usr/include -name "*" -exec grep "typedef.*pid_t" {} \;

The output looks like this:

typedef short        pid_t;        /* used for process ids */

(specifically, only the matched line is displayed (and the filename is
not)).

Question: how do I make the embedded grep display both the matched line
*and* the filename?

TIA,
Trevor Baca

 
 
 

embedded grep: how to get line and file name?

Post by Jim Mellande » Thu, 26 Nov 1998 04:00:00



> Running this command:

> grep "typedef.*pid_t" *

> Produces output like this:

> types.h:typedef short        pid_t;        /* used for process ids */

> (specifically, both the filename and matched line are displayed).

> However, when I embed essentially the same grep command as part of find like
> this:

> find /usr/include -name "*" -exec grep "typedef.*pid_t" {} \;

> The output looks like this:

> typedef short        pid_t;        /* used for process ids */

> (specifically, only the matched line is displayed (and the filename is
> not)).

> Question: how do I make the embedded grep display both the matched line
> *and* the filename?

> TIA,
> Trevor Baca


Basically, grep prints out only the matching line if given one file as
input, while when given multiple files as input, it prints out the
filename & the matching file.  I use something similar to your find/grep
combo when looking for stuff in /usr/include too, but you will get
better results if you do it this way (Also more efficient):

find /usr/include -type f -print|sort|xargs grep what-ever

The xargs will construct a large command line containing many files
which is then passed to grep, so that the multiple file behavior of grep
is invoked.

Hope this helps

 
 
 

embedded grep: how to get line and file name?

Post by Charles Dem » Thu, 26 Nov 1998 04:00:00




>Running this command:

>grep "typedef.*pid_t" *

>Produces output like this:

>types.h:typedef short        pid_t;        /* used for process ids */

>(specifically, both the filename and matched line are displayed).

>However, when I embed essentially the same grep command as part of find like
>this:

>find /usr/include -name "*" -exec grep "typedef.*pid_t" {} \;

>The output looks like this:

>typedef short        pid_t;        /* used for process ids */

>(specifically, only the matched line is displayed (and the filename is
>not)).

>Question: how do I make the embedded grep display both the matched line
>*and* the filename?

use two files in the list that grep sees.  usind /dev/null is the usual
method.  Read the man page, RTFM :-)

man grep

in your case, this should work (untested):

find /usr/include -name "*" -exec grep "typedef.*pid_t" {} /dev/null \;

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

embedded grep: how to get line and file name?

Post by Kuntal M. Daftar » Thu, 26 Nov 1998 04:00:00



> However, when I embed essentially the same grep command as part of find like
> this:

> find /usr/include -name "*" -exec grep "typedef.*pid_t" {} \;

> The output looks like this:

> typedef short        pid_t;        /* used for process ids */

> (specifically, only the matched line is displayed (and the filename is
> not)).

> Question: how do I make the embedded grep display both the matched line
> *and* the filename?

        ok, the idea is this. whenever you specify multiple files as arguments
        to grep you get the file name in the output along with the line.

        when you do find, what happens is that find calls grep with each file
        name, again and again. since it calls grep for each file it finds, grep
        gets only one file as argument - one after the other and so it does not
        show you the file name with the line.

        as a solution, you can use xargs. xargs collects the output of find and
        provides the file list found by find to grep in a single call and so
        grep will give the file name in its output

        % find /usr/include -name "*" | xargs grep "typedef.*pid_t"

        this will work well for your example, but let me discuss this further
        for sake of completeness.

        if in some other find command, if find finds just one file, again xargs
        will be able to provide only one file to grep and again grep will not
        show the file name. so in such cases, it might be required to call the
        find command thusly:

        % find /usr/include -name "elf*.h" | xargs grep "pattern" /dev/null

        in general, when you want the file name from grep along with the line
        even when there is a possibility of only one file being passed to grep
        as an argument, you can take help of the /dev/null file:

        % grep pattern * /dev/null

Kuntal Daftary

 
 
 

embedded grep: how to get line and file name?

Post by bmar.. » Fri, 27 Nov 1998 04:00:00



   >However, when I embed essentially the same grep command as part of
   >find like this:
   >find /usr/include -name "*" -exec grep "typedef.*pid_t" {} \;
   >The output looks like this:
   >typedef short        pid_t;        /* used for process ids */
   >(specifically, only the matched line is displayed (and the filename
   >is not)).
   >Question: how do I make the embedded grep display both the matched
   >line *and* the filename?

Grep displays the filename when you give it more than one filename.
find /usr/include -name "*" -exec grep "typedef.*pid_t" {} /dev/null \;

Net-Tamer V 1.08X - Test Drive

 
 
 

1. one liner which prints n lines before and m lines after the line found by grep

Hi
I am looking for a something better than this:

n=1;m=2;f=./my_file.txt;x=`grep -n my_string $f |cut -f1
-d':'`;ab="`expr $x - $n`,`expr $x + $m`"; sed -n "${ab}p" $f

Command above will print one line before and two lines after line
containing my_string in file my_file.txt.

Is there a more elegant way? Something awk based :) I guess, I am not
a big friend of awk...

Andrew

2. Iomega zip Ide and linux

3. How do I delete files w/control char embedded in file name?

4. Bochs - any experience with the Win emulator?

5. Parse file names for string and then grep these files ??

6. help with compiling mutt!

7. HELP: How do I read a file line-by-line without getting spaces stripped?

8. problem with malloc()

9. grep - getting the process name with awk (when it's over a day old)

10. getting the 2nd word from a grepped line of text

11. grep - getting the process name with awk (when it's over a day old)

12. How to make grep select both lines using only a single 'grep' command