根据xml标签内容重命名文件 [英] Rename files according to xml tag contents

查看:132
本文介绍了根据xml标签内容重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量需要重命名的XML文件.它们是.plist文件.

I have a huge amount of XML files that I need to rename. They are .plist files.

每个文件中都有一些标签,如下所示:

Within each file are a few tags, like this:

<key>Name</key>
<string>Steve Allen</string>
<key>Place</key>
<string>London</string>

目前,所有文件都使用长随机数命名,例如659238592496052.record,但我需要根据文件中的名称将它们命名为文件名,例如SteveAllen.record(无空格).

At the moment, all the files are named with long random numbers, like 659238592496052.record but I need them to be names according to the Name in the files, like SteveAllen.record (without any space).

我该怎么做?

推荐答案

for file in *.record
do
  nameline=$(grep -A 1 '<key>Name</key>' "$file" | tail -n +2) # Get line after Name key
  name=${nameline/<string>/} # Remove initial <string>
  name=${name/<\/string>/} # Remove trailing </string>
  name=${name// /} # Remove all spaces
  mv "$file" "$name".record
done

这篇关于根据xml标签内容重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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