简体   繁体   English

bash - 如何在 xml 文件的第二行之后的 append 文本?

[英]bash - how to append text after the 2nd line of a xml file?

I have a directory with around 150 subfolders.我有一个包含大约 150 个子文件夹的目录。 Each of them contains a *.xml file.它们每个都包含一个 *.xml 文件。 Now, I want to append a multiple-line text, starting after the 2nd line in all those xmls.现在,我想 append 一个多行文本,从所有这些 xml 的第二行开始。 I know works with sed command, but how do I actually do that?我知道可以使用 sed 命令,但我该怎么做呢?

with sed to insert a text after the specified second line 2a\ .使用 sed 在指定的第二行2a\之后插入文本。 -i option to edit file in place -i选项来编辑文件

sed -i "2a\\$text" file.txt

With AWK you can do:使用 AWK 您可以:

var="some text"

awk -v a="$var" 'NR != 1{ print $0, a}' file.xml

This one, append text in the end of each line after the second line of file.这一个,append 文本在文件第二行之后的每一行末尾。 But I'm not sure if this is want you want really但我不确定这是否是你真正想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM