简体   繁体   English

Shell:使用sed替换文件中的字符串

[英]shell: replacing string in a file using sed

I am writing a script to replace a string in a file in linux. 我正在编写一个脚本来替换Linux中文件中的字符串。

this is my code: 这是我的代码:

echo "Enter file name? "
read FILE1
echo "You have entered, $FILE1"

echo "String to be replaced "
read STR1
echo "You have entered, $STR1"

echo "Replace by "
read STR2
echo "You have entered, $STR2"

sed -i 's/$STR1/$STR2/g' /misc/home3/abc/$FILE1

echo "DONE !!"

The string is not getting replace in the file. 该字符串未在文件中替换。 what could be wrong with it?? 这可能是什么问题?

Thanks :) 谢谢 :)

Do not use ' for the sed-string but " like this: 不要使用'的沉渣-字符串,但"这样的:

sed -i "s/$STR1/$STR2/g" /misc/home3/abc/$FILE1

Double quotes allow the shell to do variable expansion inside of the quoted text. 双引号允许外壳程序在带引号的文本内进行变量扩展。 Single tick ' prevents this. 单剔'防止这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM