英文:
bash + how to print sequence lines in the same line
问题
我创建了以下的查找语法,以便搜索所有以 moon_
开头并以 .xml
结尾的文件,并且应该包含标签 - <Version>
。
以下是示例:
find /main_folder/kits/ -maxdepth 2 -name "moon*" -name "*.xml" -exec grep '<Version>' {} \; -exec echo {} \; | sed 'N;s/\n/ /'
请注意,我在您的语法末尾添加了一个 | sed 'N;s/\n/ /'
,这将把路径和版本信息放在同一行上。
英文:
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 ?
答案1
得分: 3
像这样:
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 {} +
或者更好的是,不要使用`grep`,使用:
xmllint --xpath '//Version/text()' "$xml"
<details>
<summary>英文:</summary>
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"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论