2013-04-22 95 views
0

我試圖插入這段文字...sed命令返回「無效的命令代碼」

"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; 

可惜我不斷收到錯誤消息「無效的命令代碼T」。 看起來sed正在將冒號解釋爲命令的一部分。 任何建議如何我可以避免這一點?

編輯: 似乎更新錯誤(使用舊文件d'oh ...) 上述表達式正常工作正如其他建議。

+0

您在開始的時候需要一個空間:'-e'5i'應該是'-e「5i' – fedorqui 2013-04-22 15:24:12

+0

@fedorqui UPS小錯字那裏。不幸的是,我仍然得到相同的錯誤:/ – Zounadire 2013-04-22 15:33:03

回答

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 
+0

如何插入一個變量到句子中? line ='「error.emailNotActivated」:$ translated' – Zounadire 2013-04-22 16:31:29

+0

@Zounadire我更新了我的答案。 – 2013-04-22 17:08:33

+1

ty給我看看有一個替代sed:D – Zounadire 2013-04-22 18:23:43

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 
+0

幾乎我在做atm:D無論如何發現我的錯誤 – Zounadire 2013-04-22 18:22:59