I am trying to output the line containing a certain pattern located in specifically named files in a unstructured directory hirearchy. Each file I'm interested in is named "foo.xml". The pattern I'm interested looks like: <digit><digit>T<digit><digit> (as a PERL regex: \d\dT\d\d). I want to output the entire line that matches this pattern (even the extra stuff on that line that doesn't match the pattern).

Where I'm at:

find . | grep foo.xml executed in the "root" gives me a list of the files I'd want to scan.

What do I do from here?

I'd much rather do this at the commandline. I could easily write a PERL script but there's probably a really straight forward way to do this.


You could try something like:

find . -name foo.xml -exec grep REGEX '{}' \;

edit In your case the REGEX is

  • so the -exec option to find executes any subsequent expressions, where '{}' is replaced by any found matches to the find command, and the command to execute is terminated by '\;'. – jmetz Jul 23 '12 at 18:35

Alternatively, since you mentioned PERL, you can use grep's -perl-regexp option:

find . -name foo.xml -exec grep -P '\d\dT\d\d' '{}' \;

Lovely feature if you are used to perl regexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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