2017-10-11 113 views
-2

由於某些原因,以下帖子中的答案對我無效。有什麼想法嗎?
how to use sed to replace a string in a file with a shell variable無法使用sed將文本替換爲shell變量

我運行CentOS的6.5

`NEW="new value" 
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}' 
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW' 
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s' 
+2

參見:[差異在bash中的單引號和雙引號之間](http://stackoverflow.com/q/6697753/3776858) – Cyrus

+1

另外sed需要filen艾姆斯作爲參議員,不需要貓。還有///'需要三個分隔符 – 123

+0

謝謝。我只是使用cat來作爲一種測試方法,而無需修改文件。 – Adoyt

回答

0

嘗試採取的關閉sed的如

$ new=N 
$ cat > j 
one 
two 
three 
$ sed -e "s/one/${new}/" j 
N 
two 
three 

一個更完整的答案嘗試this answer

+0

謝謝!用雙引號將shell變量包裝起來!像這樣: sed -e's/old /'「$ NEW」'/' – Adoyt