Click to See Complete Forum and Search --> : text processing/grep/perl?? question


JuiceWVU202
06-07-2005, 05:21 PM
I have a text file with a list of several hundred names and also several hundred text files. I need to print a list of each file and any of the names that appear in that file. I know i can cat filename | grep -i name. but how can I print the file that grep is 'grepping'

-Josh

Hayl
06-07-2005, 05:28 PM
lpr /name/of/file

JuiceWVU202
06-07-2005, 08:20 PM
maybe i wasn't clear earlier, i mean i need to output to the screen the file name as well as the names it contains. I just figured it out grep -H prints the file that the regexp was located in.

DrChuck
06-07-2005, 11:15 PM
You were clear enough - Hayl just wasn't paying attention.

drChuck

the.spike
06-08-2005, 07:36 AM
Why don't you just :

grep -i name <files>

where <files> is your list of files (or some sort of * globbing).

When grepping that way grep precedes the line that was matched with the filename that it was matched from.

Hope this helps..

spike...

int13
06-08-2005, 08:07 AM
cat file.txt | grep name > grepped.txt

Is this what you want?

bwkaz
06-08-2005, 06:43 PM
Hey, more Useless Uses of Cat! :p

grep decides whether to print the filename on each line depending on the number of filenames provided on its command line. If you're grepping through one file in one invocation of grep, it won't print the filenames. Otherwise, it will. (If you're piping the output of another command through grep, it will not print filenames, because it doesn't know them. Another reason not to use "cat file1 file2 file3" or something like that before grep. ;))

If you want to force it to print the filenames, you can add /dev/null as a file to search through. This:

grep whatever /path/to/file1 /dev/null

will print /path/to/file1 before each of the matches that were found in file1.

If you want a "header" type of thing (print the filename before each group of matches), you can do something like this:

for file in * ; do
echo "$file"
grep whatever "$file"
done You can replace the * with a list of files, or a list of glob patterns.

JuiceWVU202
06-08-2005, 08:27 PM
what i wanted was to print the file name of that had a matching line next to the matching line, the -H option for grep does this, i just must've over looked it the first time i read through the man page.