views:

205

answers:

3

I generally use perl one liners instead of grep to search through files.

For example following prints all the locations containing #include <stdio.h>

perl -ne "print if(/#include\s*[\"<]stdio.h/)" */*.[ch]

But I could not find a way to print the filename that got these lines. I tried to print $ARGV[0] but no avail.

So, how do you print the filenames that contain these lines?

+5  A: 

$ARGV is the special variable for the name of the file currently being read. I believe that is what you need.

Pinochle
+4  A: 

using ack, that is a nice replacement (written in perl) for grep:

$ ack --cc '#include'

NB

I know, I'm cheating :-P

dfa
Indeed, ack is a great grep replacement.
Pinochle
Not cheating, but sed is the proper tool to replace grep.
William Pursell
@William: huh? `sed` edits, `grep` matches. How is `sed` the proper tool to replace `grep`?
Telemachus
sed 's/grep/ack/g' :)
Arkadiy
+9  A: 

The variable that contains the name of the file that *ARGV is opened to is $ARGV, not $ARGV[0] (which is the first element of @ARGV).

print "$ARGV: $_" if /include\s*[\"<]stdio.h/;
hobbs