sed only substitutes first occurrence of string
I use sed
to create files from template files. I can't figure out, using man sed
, why it does not change all the matched strings.
If my file (template_file.txt) contains:
#!/bin/sh
#
# /etc/init.d/%SCRIPT_NAME% - Startup script for play %SCRIPT_NAME% engine
#
### BEGIN INIT INFO
[...]
EOF
Using :
sed -e "s;%SCRIPT_NAME%;script_test_name;" template_file.txt > script_test_name
Produces (script_test_nam开发者_运维技巧e) :
#!/bin/sh
#
# /etc/init.d/script_test_name - Startup script for play %SCRIPT_NAME% engine
#
### BEGIN INIT INFO
[...]
EOF
I see that for lines which have 2 times the string to replace, only the first one is replaced.
Can you give me a hint how to fix it?
The s
command changes only the first occurrence unless you add the g
(global) modifier to it.
sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name
You have to add the "g" modifier to the substitution:
sed -e "s;%SCRIPT_NAME%;script_test_name;g" template_file.txt > script_test_name
(note the last "g" in the template). This instruct sed
to substitute all the matching texts in the line.
精彩评论