68

I use the following sed command to replace text in the file:

sed -i -e 's/noreply@\(.*\).example.com/noreply@example.com/' cron.yaml

But it create backup of the file cron.yaml under name cron.yaml-e.

I tried to move -i to the end:

sed -e 's/noreply@\(.*\).example.com/noreply@example.com/' -i cron.yaml

but in this case sed returns an error.

How should I modify my command line to avoid backup file creation?

LA_
  • 19,823
  • 58
  • 172
  • 308

3 Answers3

140

According to the man page you should specify a zero length extension on macOS

sed -i '' -e 's/noreply@\(.*\).example.com/noreply@example.com/' 
jthill
  • 55,082
  • 5
  • 77
  • 137
Marco de Jongh
  • 5,270
  • 3
  • 17
  • 29
  • 3
    Ha! So much for portability! Why would Apple make this mandatory? Just so they can break other people's scripts? – chiastic-security Aug 25 '14 at 13:17
  • Apple is not the best on keeping tools compatibility with their Linux counterparts. Another example I *abominate* is `strip -x` (Mac) vs `strip --strip-unneeded` (Linux). – jweyrich Aug 25 '14 at 13:20
  • 3
    @chiastic-security http://unix.stackexchange.com/a/131940 goes more in depth on the differences and why they exist. – Marco de Jongh Aug 25 '14 at 13:22
  • Thanks. I thought that no value is equal to zero-length extension. – LA_ Aug 25 '14 at 13:26
  • 2
    @LA_ The GNU version of sed used in linux does accept no value as zero-length extension. On linux `sed -i 's/noreply@\(.*\).example.com/noreply@example.com/'` works like a charm – Marco de Jongh Aug 25 '14 at 13:29
  • 1
    @LA_ Yes, the point is that you weren't giving the `-i` flag no value; you were giving it `-e` as the value (because supplying a value seems to be mandatory on a Mac). – chiastic-security Aug 25 '14 at 14:10
  • 2
    Apple isn't doing anything wrong in this particular caee, they are simply shipping BSD `sed` which always worked like this. The various Loinux `sed` versions are a different heritage, and POSIX doesn't standardize this option at all; so both behaviors are nonstandard. – tripleee Jul 04 '20 at 06:21
5

On Windows the GNUWIN32 sed FAILS, when putting any of this:

sed -i "s/WhatToFind/WhatToPut/g" ".\MyDir\*"

sed -i.BackUp "s/WhatToFind/WhatToPut/g" ".\MyDir\*"

The BackUps are allways created on actual folder with a file name pattern of sed$$$$$$, where that six $ represents random leters and numbers.

I see no way for it to not create any BackUP at all, neither to create BackUPs that can be know to which file was the source, neither create backups on the same folder as source file.

And it also tries to read sub-folders as is they where files, of courrse showing an impossible to read message for such sub-folders; and of course it does not recurse sub-folders, it only works with all on the same folder, but not with what is on sub-folders (not recursive).

In shorts words: -i is not working at all as spected.

GNUWIN32 sed version i am using is 4.2.1, it was downloaded from: http://gnuwin32.sourceforge.net/packages/sed.htm

On Google i found a web that talks about that BUG and remomends to download a ssed instead to sed tool, i am a little afraid of not being official; link to what i found on Google about that -i BUG on sed: http://www.thinkplexx.com/learn/snippet/cmd/one-liner/working-in-place-sed-option-under-windows

Anonymous
  • 51
  • 1
  • 1
  • 3
    I got exactly the same result as you described. My workaround on Windows is to wait 1 second using ping and delete the backup file(s). – Steven Liang Aug 22 '19 at 08:02
0

Windows workaround

The accepted answer does not work for the GNUWin32 sed. I tried several variants (-i '', -i - like @Anonymous says), but it still created some backup file.

Easiest workarounds for me were to use:

  1. sed that comes with Git for Windows
  2. sed inside WSL shell.

Both worked perfectly.

m01010011
  • 895
  • 8
  • 13