Search all xml files recursively in directory for a specific tag and grep the tag’s value

command linefindxml

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:

find . -name "*.xml" -exec grep '<dbname>' {} \;             \
                     -exec echo -e {}"\n" \;                 \
                     | sed 's/<dbname>\(.*\)<\/dbname>/\1/g'

Explanation

  1. find . -name "*.xml" find all xml files recursively from current directory
  2. -exec grep '<dbname>' {} \; on each file search for pattern <dbname>
  3. -exec echo -e {}"\n" \; echo filename + new line (-e option makes echo interpret \n)
  4. | sed 's/<dbname>\(.*\)<\/dbname>/\1/g' pipe output to sed 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 for find $PWD -name ....

Related Question