0

名前を変更する必要がある XML ファイルが大量にあります。それらは .plist ファイルです。

各ファイル内には、次のようないくつかのタグがあります。

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

現時点では、すべてのファイルは長い乱数で名前が付けられていますが、ファイル659238592496052.recordの名前に従って名前を付ける必要がありますSteveAllen.record(スペースなし)。

どうすればいいですか?

4

2 に答える 2

1
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
于 2013-03-13T20:03:10.063 に答える
0

投稿したフラグメントは有効な xml ではないため、次のようにしましょう

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

このフラグメントを入力として使用すると、このプログラムは引数として指定された任意の数のファイルの名前を変更します プログラムを「myrename」として保存した場合、「myrename file1.xml file2.xml file3.xml」は file1.xml file2.xml のパス名を変更しますおよびfile3.xmlを最初のアイテムに

「キー」と呼ばれるタグは、一見魔法のように機能するようです:/

#!/usr/bin/perl

use XML::Simple;
for $file ( @ARGV[ 0 .. $#ARGV ] ) {

    my $ref = XMLin($file);

    $new_name = $ref->{'item'}->{'Name'}->{'string'};

    rename $file, "$new_name.xml" || warn "couldn't rename $file $!";
}
于 2013-03-13T20:12:11.833 に答える