私は純粋な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>