2017-10-11 127 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