Ubuntu 14.04 Trusty Tahr.
Suppose I have directory called 'testmag' which may contain 100s of xml files and directories which in turn contain many xml files as well. I don't know names of any xml files but I know one of them contains tag <dbname>....</dbname>
.
Now how to find the file containing the aforementioned tag and grep the tag's value as output in terminal
Best Answer
Here is a solution with
find
that will also output the filenames of files containing a match:Explanation
find . -name "*.xml"
find all xml files recursively from current directory-exec grep '<dbname>' {} \;
on each file search for pattern<dbname>
-exec echo -e {}"\n" \;
echo filename + new line (-e
option makes echo interpret\n
)| sed 's/<dbname>\(.*\)<\/dbname>/\1/g'
pipe output tosed
to print only the field contained between the<dbname></dbname>
tags.NOTE1: you can format output in your
echo -e ...
to have results for each file clearly laid out, e.g. by adding new lines, or lines of underscore, whatever suits your need.NOTE2: path to each file will be given relatively to
.
(e.g../subfolder1/file.xml
). If you want absolute path, go forfind $PWD -name ...
.