If you are using OSX , BSD (and AIX) versions of sed
, the backup extension for the -i
in place editing flag is not optional.
GNU sed
differs on this I believe, so the script may work on Linux.
This is a bit of a pain for portability - but it gets worse with "in-place" editing when BSD derived sed
is used. This version of sed
is arguably more "standard" in some ways (as in: "lowest common denominator across POSIX systems") but this behaviour seems like a bug:
sed: 1: "5 i hello foo": command i expects \ followed by text
Here is how I made your script work on several BSD flavors:
lnum="5"
str="Hello foo"
filename="sed-mess.txt"
sed -i "" "$lnum i\^M
$str" $filename
I had to enter a literal line end character with Ctrl-v [Return]
to get the i
command to work since \
is required and has to have nothing following it. Not sure how GNU sed
would handle this.
Can you use perl
? ;-)