BASH脚本将XML文件重命名为属性值 [英] BASH script to rename XML file to an attribute value

查看:74
本文介绍了BASH脚本将XML文件重命名为属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 很多 .xml文件,它们的结构相同:

I have a lot of .xml files structured the same way:

<parent id="idvalue" attr1="val1" attr2="val2" ...>
    <child attr3="val3" attr4="val4" ... />
    <child attr3="val5" attr4="val6" ... />
    ...
</parent>

每个文件只有一个<parent>元素,并且只有一个id属性.

Each file has exactly one <parent> element with exactly one id attribute.

所有这些文件(其中将近1,700,000个)都命名为part.xxxxx,其中xxxxx是随机数.

All of those files (almost 1,700,000 of them) are named as part.xxxxx where xxxxx is a random number.

我要根据文件内容中唯一的id属性,将每个文件命名为idvalue.xml.

I want to name each of those files as idvalue.xml, according to the sole id attribute from the file's content.

我相信使用bash脚本执行此操作将是最快,最自动化的方法.但是,如果还有其他建议,我很想听听他们的意见.

I believe doing it with a bash script would be the fastest and most automated way. But if there are other suggestions, I would love to hear them.

我的主要问题是我无法(不知道如何)在特定文件中获取idvalue,因此我可以将其与mv file.xxxxx idvalue.xml命令一起使用.

My main problem is that I am not able (don't know how) to get the idvalue in a specific file, so that I could use it with the mv file.xxxxx idvalue.xml command.

推荐答案

首先,我将使用find遍历xml文件:

First I would iterate through the xml files using find:

find -maxdepth 1 -name 'part*.xml' -exec ./rename_xml.sh {} \;

上面的行将为每个xml文件执行rename_xml.sh,并将文件名作为命令参数传递给脚本.

The line above will execute rename_xml.sh for every xml file, passing the file name as command argument to the script.

rename_xml.sh应该看起来像这样:

#!/bin/bash

// Get the id using XPath. You might probably need 
// to install xmllint for that if it is not already present.
// The xpath query will return a string like this (try it!):
//
//     id="idvalue"
//
// We are using sed to extract the value from  that
id=$(xmllint --xpath '//parent/@id' "$1" | sed -r 's/[^"]+"([^"]+).*/\1/')
mv -v "$1" "$id.xml"

别忘了

chmod +x rename_xml.sh

这篇关于BASH脚本将XML文件重命名为属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆