sed只替换字符串的第一个匹配项

sed只替换字符串的第一个匹配项,sed,Sed,我使用sed从模板文件创建文件。使用mansed,我无法理解为什么它不更改所有匹配的字符串 如果我的文件(template_file.txt)包含: #!/bin/sh # # /etc/init.d/%SCRIPT_NAME% - Startup script for play %SCRIPT_NAME% engine # ### BEGIN INIT INFO [...] EOF 使用: sed -e "s;%SCRIPT_NAME%;script_test_name;" template

我使用
sed
从模板文件创建文件。使用
mansed
,我无法理解为什么它不更改所有匹配的字符串

如果我的文件(template_file.txt)包含:

#!/bin/sh
#
# /etc/init.d/%SCRIPT_NAME% - Startup script for play %SCRIPT_NAME% engine
#
### BEGIN INIT INFO
[...]
EOF
使用:

sed -e "s;%SCRIPT_NAME%;script_test_name;" template_file.txt > script_test_name
产生(脚本测试名称):

我看到,对于有2倍字符串要替换的行,只有第一行被替换


您能给我一个如何修复它的提示吗?

除非您向其添加
g
(全局)修饰符,否则
s
命令只会更改第一次出现的内容

sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name

除非向其添加
g
(全局)修饰符,否则
s
命令仅更改第一次出现的内容

sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name
必须将“g”修饰符添加到替换:

sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name
(注意模板中的最后一个“g”)。这将指示sed替换行中所有匹配的文本。

您必须在替换中添加“g”修饰符:

sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name
(注意模板中的最后一个“g”)。这将指示
sed
替换行中所有匹配的文本