Xml 在bash中使用XPath输出多个文件
我有一个XML文件目录。每个文件都有自己的唯一标识符。每个文件还包含对其他文件的一个或多个引用(在单独的目录中),这些文件也具有唯一的ID 例如,我有一个名为Xml 在bash中使用XPath输出多个文件,xml,bash,shell,xpath,batch-processing,Xml,Bash,Shell,Xpath,Batch Processing,我有一个XML文件目录。每个文件都有自己的唯一标识符。每个文件还包含对其他文件的一个或多个引用(在单独的目录中),这些文件也具有唯一的ID 例如,我有一个名为example01.xml的文件: <file> <fileId>xyz123</fileId> <fileContents>Blah blah Blah</fileContents> <relatedFiles> <oth
example01.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>
提前感谢。类似的方法可能会奏效:
#!/bin/bash
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 $3}'); do
echo "Moving $f to ${fid}_${uid}.xml"
cp "$f" "${fid}_${uid}.xml"
done
rm "$f"
done
类似的方法可能会奏效:
#!/bin/bash
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 $3}'); do
echo "Moving $f to ${fid}_${uid}.xml"
cp "$f" "${fid}_${uid}.xml"
done
rm "$f"
done
这在示例文件中非常有效,但我使用的
@href
属性的实际值如下所示:http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234
。如何在脚本中解析属性值?明白了!只需在awk
脚本中将字段值更改为$4。感谢您的帮助。这对示例文件非常有效,但我正在使用的@href
属性的实际值如下所示:http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234
。如何在脚本中解析属性值?明白了!只需在awk
脚本中将字段值更改为$4。谢谢你的帮助。