使用 shell 脚本中的 sed 命令在 XML 文件中添加 XML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23234681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding XML element in XML file using sed command in shell script
提问by User007
I am using sed command to insert an xml element into the existing xml file.
我正在使用 sed 命令将一个 xml 元素插入到现有的 xml 文件中。
I have xml file as
我有 xml 文件
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
</Students>
I want to add new elememt as
我想添加新元素为
<student>
<name>NewName</name>
<id>NewID</id>
</student>
So my new xml file will be
所以我的新 xml 文件将是
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
<student>
<name>NewName</name>
<id>NewID</id>
</student>
</Students>
For this I have written shell script as
为此,我将 shell 脚本编写为
#! /bin/bash
CONTENT="<student>
<name>NewName</name>
<id>NewID</id>
</student>"
#sed -i.bak '/<\/Students>/ i \ "$CONTENT" /root/1.xml
sed -i.bak '/<\/Students>/ i \'$CONTENT'/' /root/1.xml
I am getting error as
我收到错误
sed: can't read <name>NewName</name>: No such file or directory
sed: can't read <id>NewID</id>: No such file or directory
sed: can't read </student>: No such file or directory
And in the xml file, only <student>is added.
The remaining elements are not added.
Does anyone know why this error?
而在xml文件中,只<student>添加了。不添加其余元素。有谁知道为什么会出现这个错误?
回答by Taher Khorshidi
change this:
改变这个:
CONTENT="<student>
<name>NewName</name>
<id>NewID</id>
</student>"
to this:
对此:
CONTENT="<student>\n<name>NewName</name>\n<id>NewID</id>\n</student>"
and then:
进而:
C=$(echo $CONTENT | sed 's/\//\\//g')
sed "/<\/Students>/ s/.*/${C}\n&/" file
回答by jaypal singh
You cannot have an unescaped newline in sed replacement text, that is $CONTENTin your example. sed uses the newline just like the shell does, to terminate a command.
您不能在 sed 替换文本中使用未转义的换行符,即$CONTENT在您的示例中。sed 就像 shell 一样使用换行符来终止命令。
If you need a newline in the replacement text, you need to precede it with a backslash.
如果需要在替换文本中使用换行符,则需要在它前面加上反斜杠。
There is another way to add text using the roption. For example:
还有另一种使用该r选项添加文本的方法。例如:
Lets say your main file is;
假设您的主文件是;
$ cat file
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
</Students>
You text you want to add is in another file (not variable):
您要添加的文本位于另一个文件(非变量)中:
$ cat add.txt
<student>
<name>NewName</name>
<id>NewID</id>
</student>
You can do (using gnu sed):
你可以做(使用gnu sed):
$ sed '/<\/Students>/{
r add.txt
a \</Students>
d
}' file
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
<student>
<name>NewName</name>
<id>NewID</id>
</student>
</Students>
However, having given this option, it is still a very bad idea to parse xml with regular expression. It makes the solution very fragile and easy to break. Consider this as a learning exercise only.
然而,给出了这个选项,用正则表达式解析 xml 仍然是一个非常糟糕的主意。它使解决方案非常脆弱且容易破裂。将此视为仅作为学习练习。
回答by potong
This might work for you (GNU sed & Bash):
这可能对你有用(GNU sed & Bash):
CONTENT=' <student>\
<name>NewName</name>\
<id>NewID</id>\
</student>'
sed '/<\/Students>/i\'"$CONTENT" file
Alternatively, put the new students in a file and:
或者,将新学生放在一个文件中,然后:
sed '/<\/Students>/e cat new_student_file' file

