これは、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>
前の質問への回答により、これらのファイルの大部分を正常に処理できました。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 を使用して、特定の要素を含まないファイルを除外するにはどうすればよいですか? 前もって感謝します。