Sed命令返回“无效命令代码”

时间:2013-04-22 15:22:28

标签: sed

我正在尝试插入此文字......

"error.emailNotActivated":"This email address has not been activated yet."

...使用sed在第5行。

到目前为止,这是我的命令

translated="This email address has not been activated yet.";
sed -i '' -e '5i\'$'\n''"error.emailNotActivated":'"\"$translated\"" local.strings;

我很遗憾地收到错误消息“无效的命令代码T”。 似乎sed将冒号解释为命令的一部分。 有什么建议我可以避免这个吗?

编辑: 似乎更新错误(使用旧文件哦...) 上面的表达式和其他建议一样正常。

2 个答案:

答案 0 :(得分:3)

为什么你为此与sed战斗?这在awk中是微不足道的:

awk -v line='"error.emailNotActivated":"'"$translated"'"' '
NR==5{print line} {print}
' file

或:

awk -v line="\"error.emailNotActivated\":\"${translated}\"" '
NR==5{print line} {print}
' file

答案 1 :(得分:1)

你在找这样的东西吗?

$ seq 1 5 > file

$ cat file
1
2
3
4
5

$ translated="\"error.emailNotActivated\":\"This email address has not been activated yet.\""

$ echo $translated 
"error.emailNotActivated":"This email address has not been activated yet."

$ sed -i "5i $translated" file

$ cat file
1
2
3
4
"error.emailNotActivated":"This email address has not been activated yet."
5