Bash 根据 XML 重命名文件
Bash rename files based on XML
我正在制作一个 bash 脚本,我需要重命名特定文件类型(在本例中为 svg)的所有文件,以反映它们在xml 文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manifest identifier="id1" version="2006-01" smartnotebook:filesource="SMART Notebook for Mac Version=11.3.804.0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:smartnotebook="http://www.smarttech.com/2006-01/notebook" xmlns:smartgallery="http://www.smarttech.com/2006-01/gallery"><metadata><schema>ADL SCORM</schema><schemaversion>CAM 1.3</schemaversion><adlcp:location>metadata.xml</adlcp:location></metadata><organizations><organization id="pagegroups"><item id="group0" identifierref="group0_pages"><title>Group 1</title></item></organization></organizations><resources><resource identifier="group0_pages" href="page4.svg" type="webcontent" adlcp:scormType="asset">*******<file href="page4.svg"/><file href="page0.svg"/><file href="page1.svg"/><file href="page3.svg"/><file href="page2.svg"/>********</resource><resource identifier="pages" href="page4.svg" type="webcontent" adlcp:scormType="asset"><file href="page4.svg"/><file href="page0.svg"/><file href="page1.svg"/><file href="page3.svg"/><file href="page2.svg"/></resource><resource identifier="images"/><resource identifier="sounds"/><resource identifier="attachments"/><resource identifier="flash"/><resource identifier="videos"/><resource identifier="annotationmetadata"/><resource identifier="brush"/></resources></manifest>
我需要它来读取此文件(以及相同格式的其他文件)和(例如)将 page4.svg 重命名为 file0.svg ... 并将 page0.svg 重命名为 file1.svg。我一直在研究如何通过 xmllint 执行此操作,但我对 xpath 的了解非常有限。什么都有用!
谢谢!
xpath 查询 //resource/file/@href
将为您提供 href 列表。但是会有基于 xml 样本的重复项。如果您只想要来自第一个资源的列表,您可以使用 //resource[1]/file/@href
或通过标识符指定为 //resource[@identifier="group0_pages"]/file/@href
.
但是,出于某种原因,我无法让 xmllint 接受这些查询,即使它们是正确的并且可以在其他工具中使用。相反,在 Linux 中,我使用 xpath
实用程序,它是 libxml-xpath-perl
包中的一个 perl 脚本。除了一些简单的解析,一个示例 bash 脚本如下所示:
i=0
while read filename; do
mv $filename file$i.svg
let i++
done < <(xpath -q -e '//resource[@identifier="group0_pages"]/file/@href' input.xml | cut -d\" -f2)
我正在制作一个 bash 脚本,我需要重命名特定文件类型(在本例中为 svg)的所有文件,以反映它们在xml 文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manifest identifier="id1" version="2006-01" smartnotebook:filesource="SMART Notebook for Mac Version=11.3.804.0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:smartnotebook="http://www.smarttech.com/2006-01/notebook" xmlns:smartgallery="http://www.smarttech.com/2006-01/gallery"><metadata><schema>ADL SCORM</schema><schemaversion>CAM 1.3</schemaversion><adlcp:location>metadata.xml</adlcp:location></metadata><organizations><organization id="pagegroups"><item id="group0" identifierref="group0_pages"><title>Group 1</title></item></organization></organizations><resources><resource identifier="group0_pages" href="page4.svg" type="webcontent" adlcp:scormType="asset">*******<file href="page4.svg"/><file href="page0.svg"/><file href="page1.svg"/><file href="page3.svg"/><file href="page2.svg"/>********</resource><resource identifier="pages" href="page4.svg" type="webcontent" adlcp:scormType="asset"><file href="page4.svg"/><file href="page0.svg"/><file href="page1.svg"/><file href="page3.svg"/><file href="page2.svg"/></resource><resource identifier="images"/><resource identifier="sounds"/><resource identifier="attachments"/><resource identifier="flash"/><resource identifier="videos"/><resource identifier="annotationmetadata"/><resource identifier="brush"/></resources></manifest>
我需要它来读取此文件(以及相同格式的其他文件)和(例如)将 page4.svg 重命名为 file0.svg ... 并将 page0.svg 重命名为 file1.svg。我一直在研究如何通过 xmllint 执行此操作,但我对 xpath 的了解非常有限。什么都有用! 谢谢!
xpath 查询 //resource/file/@href
将为您提供 href 列表。但是会有基于 xml 样本的重复项。如果您只想要来自第一个资源的列表,您可以使用 //resource[1]/file/@href
或通过标识符指定为 //resource[@identifier="group0_pages"]/file/@href
.
但是,出于某种原因,我无法让 xmllint 接受这些查询,即使它们是正确的并且可以在其他工具中使用。相反,在 Linux 中,我使用 xpath
实用程序,它是 libxml-xpath-perl
包中的一个 perl 脚本。除了一些简单的解析,一个示例 bash 脚本如下所示:
i=0
while read filename; do
mv $filename file$i.svg
let i++
done < <(xpath -q -e '//resource[@identifier="group0_pages"]/file/@href' input.xml | cut -d\" -f2)