正确的语法如下。
解决方案一:用于简单的字符串替换。
sed 's/old_text/new_text/' Input_file
你不需要提到 echo 和所有那里。
解决方案 2: 如果您想搜索一个字符串,然后替换任何字符串,那么下面的内容可能会对您有所帮助。
sed '/look_for_string/s/old_text/new_test/' Input_file
解决方案 3: 如果您想将任何字符串的多次/所有出现更改为一行,那么在命令的最后添加 g 也可以帮助您。
sed '/look_for_string/s/old_text/new_text/g' Input_file
第 4 种解决方案:如果您想对 Input_file 本身进行更改,那么以下内容可能会对您有所帮助。
sed -i '/look_for_string/s/old_text/new_text/g' Input_file
EDIT1:根据 OP,代码应将变量的值替换为行,然后以下可能会帮助您。
VAL="SINGH"
sed 's/tools/'"$VAL"'/' Input_file
EDIT2: 为 OP 添加一个示例,用于在匹配字符串时替换字符串的第 n 个数字。
假设以下是我的 Input_file。
cat Input_file
31116441
AAA,hi,hi,hi,hji
AAA
AA
BBB
BB
现在用第 3 次出现的 string(hi) 代替由 AAA 星标的行,我们可以按照下面的方法进行。
sed '/AAA/s/hi/cha_cha_ch_ch_cha/3' Input_file
31116441
AAA,hi,hi,cha_cha_ch_ch_cha,hji
AAA
AA
BBB
BB