我有一个包含数百个地标的kml文件,我需要将每个地标移动到它自己的文件夹中。 在这个过程中,我需要能够通过它的地标名称命名/重命名每个文件夹。 这可以通过使用awk,perl或sed来完成吗?我不是XML专家。
kml采用以下形式:
<Folder>
<name>TEST</name>
<open>1</open>
<Placemark>
<name>LOCATION ONE</name>
[...]
</Placemark>
<Placemark>
<name>LOCATION TWO</name>
[...]
</Placemark>
<Placemark>
<name>LOCATION ONE & TWO</name>
[...]
</Placemark>
</Folder>
它需要是这样的:
<Folder>
<name>TEST</name>
<open>1</open>
<Folder>
<name>LOCATION ONE</name>
<Placemark>
<name>LOCATION ONE</name>
[...]
</Placemark>
</Folder>
<Folder>
<name>LOCATION TWO</name>
<Placemark>
<name>LOCATION TWO</name>
[...]
</Placemark>
</Folder>
<Folder>
<name>LOCATION ONE & TWO</name>
<Placemark>
<name>LOCATION ONE & TWO</name>
[...]
</Placemark>
</Folder>
</Folder>
到目前为止,我设法使用以下awk命令按<placemark>
标记包装每个<folder>
标记,但我还需要能够通过其地标名称重命名每个文件夹:
awk '/<Placemark>/{system("cat file1");next}1' file.kml
awk '/<\/Placemark>/{system("cat file2");next}1' file.kml
文件1:
<Folder>
<name>@@FOLDER@@</name>
<Placemark>
file2的
</Placemark>
</Folder>
答案 0 :(得分:1)
这个脚本可以帮到你:
# Expects the file in reverse
# tac file.xml | awk -f parse_kml.awk | tac
/<\/Placemark>/ {
p_flag = 1 # Set flag if inside tag
print "</Folder>" # Print closing folder tag
print $0 # Print current line
next # Skip to next line
}
/<Placemark>/ {
print $0 # Print current line
print line # Print name tag
print "<Folder>" # Print folder tag
next # Skip to next line
}
p_flag && /<name>/ {
line=$0 # Store the line
}
{
print $0
}
保存到parse_kml.awk
等文件并运行行:
tac file.xml | awk -f parse_kml.awk | tac
如果你有xmllint
,你可以使用这种格式输出:
$ tac file.xml | awk -f parse_kml.awk | tac | xmllint --format -
<?xml version="1.0"?>
<Folder>
<name>TEST</name>
<open>1</open>
<Folder>
<name>LOCATION ONE</name>
<Placemark><name>LOCATION ONE</name>
[...]
</Placemark>
</Folder>
<Folder>
<name>LOCATION TWO</name>
<Placemark><name>LOCATION TWO</name>
[...]
</Placemark>
</Folder>
<Folder>
<name>LOCATION ONE & TWO</name>
<Placemark><name>LOCATION ONE & TWO</name>
[...]
</Placemark>
</Folder>
</Folder>