1

I want to do the following:

cat *.xml | grep some_string_here

This tells me if a particular string exists in an xml file in a directory. Unfortunately it does not tell me which file.

how can I do this better? cat command does not see me to have an switch that adds a filename prefix to the output...

4 Answers4

7

cat is unnecessary (UUOC!) grep will normally tell you which file the matched line was found in when used like this:

   grep some_string_here *.xml

You can also use the -H switch to always to this:

   grep -H some_string_here *.xml
Cakemox
  • 25,209
  • 6
  • 44
  • 67
2

You need to do:

grep some_string_here *.xml

instead, then grep will automatically prepend the filename to each match.

Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52
2

Just use grep

grep some_string *.xml

The output will be something like

a.xml: string containing some_string
xyzzy.xml:some_string in a different line

If a file contains more than one occurrence of some_string each occurrence will be printed. If you use

grep -l some_string *.xml

only the filename will be printed.

user9517
  • 115,471
  • 20
  • 215
  • 297
0
grep some_string_here /dev/null *.xml

... is a small improvement over the other suggestions above, since it makes sure that the name of the file containing the match is always printed, even if the current directory only contains one XML file.

James Youngman
  • 344
  • 1
  • 8