1

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

1
  • 1
    Note to future readers: the question is tagged osx, which natively comes with BSD sed, but the question is explicitly about GNU sed, so it's the latter's behavior that matters, and the platform is incidental. Commented Apr 28, 2015 at 3:49

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great follow up mklement0, thanks for the clarification. upvoted
@mklement0 Updated. I held off for a bit because I wasn't sure how best to preserve the thrust of the answer and incorporate the additional information without making my answer worse than tadman's answer. I think I managed that fairly well this way though.
@mklement0 Updated again. Yes, -e is optional in this case but I tend to prefer using it to be explicit (though without - being a valid script starting character that's much less necessary of a default position than using -- with various commands or -e with grep).
(I've cleaned up my other comments; answer looks great now.) Just to add some info: The reason that in BSD sed you cannot directly adjoin the mandatory '' (to specfiy an empty suffix) to -i is a technical one: if you specify -i'', the shell performs quote removal, so what BSD sed actually sees is just -i, causing it to misinterpret the next argument as the suffix.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.