这是 previous question 的后续行动关于在 bash 中使用 XPath。
我有一组 XML 文件,其中大部分编码与其他文件的关系:
<file>
<fileId>xyz123</fileId>
<fileContents>Blah blah Blah</fileContents>
<relatedFiles>
<otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234'>
<title>Some resource</title>
</otherFile>
<otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=4321'>
<title>Some other resource</title>
</otherFile>
</relatedFiles>
</file>
previous question 的答案帮助我成功处理了其中的大部分文件。但是,集合中有一些文件不包含任何 relatedFiles/otherFile
元素。我希望能够单独处理这些文件并将它们移动到“其他”文件夹中。我以为我可以使用 XPath not()
函数来执行此操作,但是当我运行脚本时,我收到该行的“找不到命令”错误。
#!/bin/bash
mkdir other
for f in *.xml; do
fid=$(xpath -e '//fileId/text()' "$f" 2>/dev/null)
for uid in $(xpath -e '//otherFile/@href' "$f" 2>/dev/null | awk -F= '{gsub(/"/,"",$0); print $4}'); do
echo "Moving $f to ${fid:3}_${uid}.xml"
cp "$f" "${fid:3}_${uid}.xml"
done
if $(xpath -e 'not(//otherFile)' "$f" 2>/dev/null); then
echo "Moving $f to other/${fid:3}.xml"
cp "$f" "other/${fid:3}.xml"
fi
rm "$f"
done
如何在 bash 中使用 XPath 过滤掉不包含某些元素的文件?提前致谢。
最佳答案
$()
构造替换命令的输出。因此,无论 xpath
吐出什么都将被替换,shell 将尝试将其作为命令执行,这就是您收到错误消息的原因。
由于 xpath
似乎没有根据是否找到节点提供不同的退出代码,您可能只需要将输出与某些内容进行比较,或者测试是否为空:
if [ -z "$(xpath -q -e '//otherFile' "$f" 2>/dev/null)" ]; then
如果 xpath
没有输出,这应该执行以下代码。要颠倒意义,请使用 -n
而不是 -z
(不确定您想要哪个)。
关于xml - 在带有 not() 的 bash 中使用 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082066/