SED without backup file
Asked Answered
E

3

73

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

sed -i -e 's/noreply@\(.*\).example.com/[email protected]/' 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/[email protected]/' -i cron.yaml

but in this case sed returns an error.

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

Eliathan answered 25/8, 2014 at 13:7 Comment(1)
you don't need to put -eDomicile
C
152

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

sed -i '' -e 's/noreply@\(.*\).example.com/[email protected]/' 
Cartwright answered 25/8, 2014 at 13:8 Comment(7)
Ha! So much for portability! Why would Apple make this mandatory? Just so they can break other people's scripts?Wolford
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).Quagmire
@Wolford unix.stackexchange.com/a/131940 goes more in depth on the differences and why they exist.Cartwright
Thanks. I thought that no value is equal to zero-length extension.Eliathan
@Eliathan The GNU version of sed used in linux does accept no value as zero-length extension. On linux sed -i 's/noreply@\(.*\).example.com/[email protected]/' works like a charmCartwright
@Eliathan 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).Wolford
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.Anthropoid
D
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

Delgado answered 13/4, 2018 at 10:38 Comment(1)
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).Valeryvalerye
Z
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.

Zennie answered 14/10, 2021 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.