使用bash脚本,我试图在文件中插入一行(最终会有4条额外的行,一个接一个)。
我正在尝试通过iiSeymour对线程执行答案:
我认为这与dgibbs在他自己的主题中所做的评论相同:
我要在其后插入新文本的行很长,因此我首先将其保存在变量中:
field1=$(head -2 file847script0.xml | tail -1)
我要插入的文本是:
insert='newtext123'
运行时:
sed -i".bak" "s/$field1/$field1\n$insert/" file847script0.xml
我得到错误:
sed: 1: "s/<ImageAnnotation xmln ...": bad flag in substitute command: 'c'
我也试着按照线程
但是命令
sed -i".bak" "s/\/$field1/$field1\n$insert/" file847script0.xml
仍然给我同样的错误:
sed: 1: "s/\/<ImageAnnotation xm ...": bad flag in substitute command: 'c'
我正在使用Mac OS X 10.5。
知道我在做什么错吗?谢谢!
悲伤,请使用awk。无需担心替换文本中的特殊字符或随机的单字符命令和标点符号。
在这种情况下,您似乎所需要做的就是在第二行之后打印一些新文本,这就是:
$ cat file
a
b
c
$ insert='absolutely any text you want, including newlines
slashes (/), backslashes (\\), whatever...'
$ awk -v insert="$insert" '{print} NR==2{print insert}' file
a
b
absolutely any text you want, including newlines
slashes (/), backslashes (\), whatever...
c
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句