Вот скрипт на 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>
С GNU awk для соответствия 3-го аргумента():
$ awk 'match($0,/([^=]+)(.*)/,a){ gsub(/\./,"_",a[1]); print "KAFKA_" toupper(a[1]) a[2]}' file
KAFKA_DELETE_TOPIC_ENABLE=true
KAFKA_BROKER_ID=-1
KAFKA_ADVERTISED_LISTENERS=null
С любым awk:
$ awk 'match($0,/[^=]+/){ tag=substr($0,1,RLENGTH); gsub(/\./,"_",tag); print "KAFKA_" toupper(tag) substr($0,1+RLENGTH)}' file
KAFKA_DELETE_TOPIC_ENABLE=true
KAFKA_BROKER_ID=-1
KAFKA_ADVERTISED_LISTENERS=null
awk -F "=" 'OFS="="{gsub(/\./,"_",$1);print "KAFKA_"toupper($1)"="$2}' FILE
ВЫХОД
KAFKA_DELETE_TOPIC_ENABLE=true
KAFKA_BROKER_ID=-1
KAFKA_ADVERTISED_LISTENERS=nul1
питон
#!/usr/bin/python
import re
k=open('file1','r')
for i in k:
y=i.split('=')
z=y[0].upper().replace(".","_")+"="+y[1]
print "KAFKA_{0}".format(z).strip()
выход
KAFKA_DELETE_TOPIC_ENABLE=true
KAFKA_BROKER_ID=-1
KAFKA_ADVERTISED_LISTENERS=null