<source file='/home/anpham/Projects/vm-builder/30G'/>
Run Code Online (Sandbox Code Playgroud)
我尝试使用 sed 命令从上面的输出中提取绝对路径,但输出没有修剪字符串。以下命令是我的尝试。
virsh dumpxml --domain "test1" | grep 'source file' | head -1 | sed '/^<source$/d'
Run Code Online (Sandbox Code Playgroud)
输出仍然是:
<source file='/home/anpham/Projects/vm-builder/30G'/>
Run Code Online (Sandbox Code Playgroud)
要提取XML 文档file
中所有source
节点的所有属性的值,您可以这样使用xmlstarlet
:
xmlstarlet sel -t -v '//source/@file' -nl file.xml
Run Code Online (Sandbox Code Playgroud)
或者,从您的virsh
命令中读取:
virsh dumpxml --domain "test1" | xmlstarlet sel -t -v '//source/@file' -nl
Run Code Online (Sandbox Code Playgroud)
您的管道的问题在于最终sed
命令会尝试删除包含确切字符串<source
(没有其他任何内容)的所有行。您在 的输入中没有这样的行sed
。