0

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

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

...在第 5 行使用 sed。

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

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

不幸的是,我不断收到错误消息"invalid command code T"。sed 似乎将冒号解释为命令的一部分。有什么建议我可以避免这种情况吗?

编辑:似乎是一个更新错误(使用旧文件 d'oh ...),上面的表达式和其他建议一样工作正常。

4

2 回答 2

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
于 2013-04-22T16:18:24.257 回答
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
于 2013-04-22T15:53:17.900 回答