bash + 如何在同一行上打印序列行

huangapple go评论58阅读模式
英文:

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 - &lt;Version&gt;

here is example

find   /main_folder/kits/  -maxdepth 2  -name &quot;moon*&quot; -name &quot;*.xml&quot; -exec grep &#39;&lt;Version&gt;&#39; {} \;    -exec echo {} \;
 
    &lt;Version&gt;1.3.12-dev.137&lt;/Version&gt;
/main_folder/kits/A/moon_aaa.xml
    &lt;Version&gt;2.1.1-dev.13&lt;/Version&gt;
/main_folder/kits/B/moon_bbb.xml
        &lt;Version&gt;1.0.144&lt;/Version&gt;
/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 &quot;moon*&quot; -name &quot;*.xml&quot; -exec grep &#39;&lt;Version&gt;&#39; {} \;    -exec echo {} \; | .........
 
    
/main_folder/kits/A/moon_aaa.xml &lt;Version&gt;1.3.12-dev.137&lt;/Version&gt;
/main_folder/kits/B/moon_bbb.xml &lt;Version&gt;2.1.1-dev.13&lt;/Version&gt;
/main_folder/kits/C/moon_ccc.xml &lt;Version&gt;1.0.144&lt;/Version&gt;

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 &#39;moon*&#39; -name &#39;.xml&#39; -exec bash -c &#39;
        for xml; do 
            res=$(grep &quot;&lt;Version&gt;&quot; &quot;$xml&quot;) &amp;&amp; echo &quot;$1 $res&quot;
        done
    &#39; bash {} +

Or better, instead of `grep`, use :

    xmllint --xpath &#39;//Version/text()&#39; &quot;$xml&quot;

</details>



huangapple
  • 本文由 发表于 2023年3月7日 18:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定