Issue
I created the following find syntax in order to search all files that start with moon_
and ended with .xml
and should contain the tag - <Version>
here is example
find /main_folder/kits/ -maxdepth 2 -name "moon*" -name "*.xml" -exec grep '<Version>' {} \; -exec echo {} \;
<Version>1.3.12-dev.137</Version>
/main_folder/kits/A/moon_aaa.xml
<Version>2.1.1-dev.13</Version>
/main_folder/kits/B/moon_bbb.xml
<Version>1.0.144</Version>
/main_folder/kits/C/moon_ccc.xml
as above its print the path and the version
but we want to print is better so path and version will be in the same line like the following example
find /main_folder/kits/ -maxdepth 2 -name "moon*" -name "*.xml" -exec grep '<Version>' {} \; -exec echo {} \; | .........
/main_folder/kits/A/moon_aaa.xml <Version>1.3.12-dev.137</Version>
/main_folder/kits/B/moon_bbb.xml <Version>2.1.1-dev.13</Version>
/main_folder/kits/C/moon_ccc.xml <Version>1.0.144</Version>
what we need to add in our syntax in order to print both in the same line ?
Solution
Like this:
find /main_folder/kits/ -maxdepth 2 -name 'moon*' -name '.xml' -exec bash -c '
for xml; do
res=$(grep "<Version>" "$xml") && echo "$1 $res"
done
' bash {} +
Or better, instead of grep
, use :
xmllint --xpath '//Version/text()' "$xml"
Answered By - Gilles Quénot Answer Checked By - Mary Flores (WPSolving Volunteer)