我无法访问纯 Linux 盒子。我有一堆缺少文件名标注行的 XML 文件。我需要将该行插入到 XML 文件中的特定位置,并通过一个小的转换根据 XML 文件的文件名生成文件名标注。
例子:
24ToLife_AFamilyDivided_191045_DANY.xml 有
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:rating>TV-14</media:rating>
我需要它来阅读:
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:content url="24ToLife_AFamilyDivided_191045.mpg" type="video/mpg" expression="full" />
<media:rating>TV-14</media:rating>
答案1
我刚刚在 MacOS High Sierra 中编写并测试:
#!/bin/sh
for fl in *.xml
do
filename=$(echo $fl | cut -f 1 -d '.' | sed 's/_DANY$//')
sed -i .orig '1a\
<media:content url="'$filename'.mpg" type="video/mpg" expression="full" /> \
' $fl
done
ls *.xml search in current directory
-i .orig backup of original files with suffix
'1a ..' insert into second line
sed
MacOS 中的BSD与 GNU 有一些不同sed
,因此以下表达式必须写在单独的行中:
'1a \ # backslash and newline
some text'
换行符\n
无法识别,所以你应该写:
'1a \
some text # newline here
'
代替:
'1a \
some text\n'
用法:
yurijs-MacBook-Pro:sed yurij$ cat *.xml
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:rating>TV-14</media:rating>
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:rating>TV-14</media:rating>
yurijs-MacBook-Pro:sed yurij$ ./cli
yurijs-MacBook-Pro:sed yurij$ cat *.xml
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:content url="24ToLife_AFamilyDivided_191045.mpg" type="video/mpg" expression="full" />
<media:rating>TV-14</media:rating>
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:content url="tt.mpg" type="video/mpg" expression="full" />
<media:rating>TV-14</media:rating>
答案2
这是一个 python 脚本,应该可以完成您想要的操作:
#!/usr/bin/env python
# -*- encoding: ascii -*-
"""insert_xml.py"""
import sys
from bs4 import BeautifulSoup as Soup
# Get the filename from the command-line
filename = sys.argv[1]
with open(filename, 'r') as xmlfile:
# Parse the file
soup = Soup(xmlfile.read(), "html.parser")
# Search for "description" tags
for element in soup.findAll("description"):
# Check to see if the "media:content" element is missing
if element and not element.find_next_sibling("media:content"):
# If so, construct a new "media:content" tag
new_tag = soup.new_tag('media:content')
new_tag["url"] = filename
new_tag["type"] = "video/mpg"
new_tag["expression"] = "full"
# Insert the "media:content" tag after the "description" tag
element.insert_after(new_tag)
# Print the modified XML document - one element per line
for element in soup.findAll():
print(element)
下面是它的实际效果:
$ python insert_xml.py in.xml
<description>Entrepreneur James overcame unconscionable childhood abuse before the sins of his past came back to haunt him.</description>
<media:content expression="full" type="video/mpg" url="in.xml"></media:content>
<media:rating>TV-14</media:rating>