Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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...

share|improve this question
    
Migrate to unix se site? –  mattdm Jan 15 '11 at 17:32
add comment

4 Answers 4

up vote 7 down vote accepted

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
share|improve this answer
    
Thanks for bringing UUOC to my attention, I feel like a right noob :-) –  DutchUncle Jan 15 '11 at 17:41
    
LOL, I'd give you an extra +1 if I could for "UUOC". –  JakeRobinson Jan 16 '11 at 0:09
add comment

You need to do:

grep some_string_here *.xml

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

share|improve this answer
add comment

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.

share|improve this answer
    
-i is "ignore case" in the versions of grep that I know about. Some versions use -m to stop after one match. –  Dennis Williamson Jan 15 '11 at 18:00
add comment
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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.