Reputation: 8375
Not sure whats going on, I replaced with gnu sed but I am getting backup files somehow. This is exactly what I am doing
mkdir tmp && cd $_
echo 'test' > test.txt
ls
test.txt
sed -ie 's/test/replaced/g' test.txt
ls
test.txt
test.txte
What's going on here and how do I prevent this? It should edit in place, not create a backup
sed version (GNU sed) 4.2.2
Upvotes: 1
Views: 80
Reputation: 80961
As per the fantastic comments from mklement0 the POSIX spec tells us that:
With optional option-arguments, POSIX utility conventions require that (emphasis his) "a conforming application shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening characters.
Since GNU sed considers the suffix argument to -i
to be optional it requires it to be cuddled up against the option argument so when you write -ie
GNU sed interprets that as requesting a suffix of e
for the -i
argument. (BSD sed would interpret it in the same manner for reasons that are explained in the additional info at the bottom.)
What this all means is that you need to use -i -e
to get the behavior you want (for GNU sed) instead (for BSD sed you would need -i '' -e
).
Additional details about an unfortunate but interesting distinction between GNU sed and BSD sed:
GNU sed and BSD sed (OSX sed) disagree on whether the suffix value for the -i
argument is optional or mandatory.
This matters because complementing the POSIX requirement above we find the following in the spec as well (emphasis mkelement0's again):
an option with a mandatory option-argument [...], a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters.
GNU sed considers the suffix to be optional (and this causes the behavior above) because it accepts the cuddled e
for the optional argument but ignores the separated -e
(or anything else) as a separate argument.
BSD sed considers the suffix mandatory (even though it may be empty) this then implies that the option should be separated from the flag with a space (e.g. -i .bak
or -i ''
) though as the "However" note indicates, BSD sed also allows any non-empty suffix to be cuddled up against the -i
flag.
This disagreement, as mklement0 points out and which comes up on SO every now and then, means that you cannot use an empty in-place edit suffix in a manner that is portable to both GNU and BSD versions of sed.
Upvotes: 3
Reputation: 211610
As always, the man
page helps:
-i extension
:
Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero-length extension when in-place edit
ing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.
Your use of -ie
adds e
to the end. Remove that.
Upvotes: 0