The slashes in the replacement text confuse sed
about what's part of the s///
command and what is pathname. The fix is easy — use a different character to mark the parts of the substitute command:
sed "s%feat_files(1) $line_old%feat_files(1) $line_new%" $file
This uses a %
in place of the /
; you could use any character that doesn't appear in the file names. Consider Control-A or another control character if you think %
might appear some time.
You can even change the character marking the ends of a searching (matching) regex if need be:
sed "\\%feat_files(1) $line_old%,\\%feat_files(1) $line_new% { … }"
to match the lines between the patterns in $line_old
and $line_new
.
I use double quotes because of the variables. An alternative is to do what the question started to do, and use single quotes, but you should still surround the variables in double quotes:
sed 's%feat_files(1) '"$line_old"'%feat_files(1) '"$line_new"'%' $file
The code in the question must be running on a Mac with the BSD sed
. The GNU sed
variant doesn't like the empty argument after the -i
option; you specify just -i
with GNU sed
to do in-situ replacement. For portability, you need to use -i.bak
with a non-empty suffix attached to the -i
option — and you have to remove the backup.
Don't use the -i
option (with either version of sed
) until you've got the basics working. It can wreck your original file if you aren't careful. (Of course, you're probably working on a copy of the original while testing, but having to restore the file to a known state before each test is unnecessary work, in my book. It's much easier just to leave the file unchanged until you know the script will work.)