2

I'm trying to get sed to replace a line in a file with the contents of another file. Got this to work, however the in-place replacement somehow produces an extra file with -e suffixed.

This only seems to happens on macOS (High Sierra), and doesn't happen on Linux (Alpine) as I tried to reproduce this in a docker container.

My commands that reproduce this in sequence:

$ echo 'someline' > target_file.txt
$ echo 'replacementcontent' > replacement.txt
$ sed -Ei -e "\#^someline\$#{
r replacement.txt
d
}" target_file.txt
$ cat target_file.txt
replacementcontent
$ ls
replacement.txt   target_file.txt   target_file.txt-e

The replacement worked as intended but in a Linux environment the target_file.txt-e would not be there.

I know there are differences between the macOS and Linux sed, but this just seems random but I'm likely just not understanding something.

Why does this happen, and can the command be written in an agnostic way (so that it works the same on both macOS and Linux)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sg3s
  • 9,411
  • 3
  • 36
  • 52
  • 2
    It would seem that you need to supply an argument to the `-i` option, perhaps `''`? – potong Aug 08 '18 at 09:33
  • @potong Yep, thats it. Please post that as an answer. Overlooked that in the manpage. – sg3s Aug 08 '18 at 09:35
  • see also: [sed in-place flag that works both on Mac (BSD) and Linux](https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux) and [BSD/macOS Sed vs GNU Sed vs the POSIX Sed specification](https://stackoverflow.com/questions/24275070/sed-not-giving-me-correct-substitute-operation-for-newline-with-mac-difference/) .. such common issues and other resources are part of tag wiki: https://stackoverflow.com/tags/sed/info – Sundeep Aug 08 '18 at 09:46

0 Answers0