首页 > 解决方案 > 在 XML 中搜索所有子目录的值并返回子目录名称和值

问题描述

例子

目录路径:/this/dir/contains/

子目录:sub1;子2;子3

在每个子目录中都有一个 xml:hello.XML

/this/dir/contains/sub1/hello.XML

在 hello.XML 中有一个如下所示的字符串: Zipper="YYZ"

请注意,每个 hello.XML 中的值“YYZ”可能不同

我可以使用 grep 在每个 hello.XML 中返回子目录名称和“Zipper”值吗

标签: grep

解决方案


使用选项“ -R ”递归搜索列出的子目录。

bash-3.2$ pwd
/Users/anku/Documents/project
bash-3.2$ cat sub1/sub1.xml
Some line
Zipper="HELLO1"
some line
bash-3.2$ 
bash-3.2$ cat sub2/sub2.xml
Some line
Zipper="HELLO2"
some line
bash-3.2$ cat sub3/sub3.xml
Some line
Zipper="HELLO3"
some line
bash-3.2$ 
bash-3.2$ grep -Ri "Zipper" *
sub1/sub1.xml:Zipper="HELLO1"
sub2/sub2.xml:Zipper="HELLO2"
sub3/sub3.xml:Zipper="HELLO3"
bash-3.2$ 

请在询问时也添加您的努力。祝你好运


推荐阅读