1

I am using sed command in a shell script to edit & replace some file content of an xml file in a osx(unix) environment. Is there some way I can avoid sed creating the temporary files with -e ? I am doing the following in my script to edit a file with sed.

sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml

Above sed command works great but creates an extra file as /PathTo/MyEditableFile.xml-e

How can I avoid creation of the the extra file with -e there ?

I tried some options like setting a temporary folder path to sed so that it creates the temporary file in /tmp/. Like so:

sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml >/tmp

But doesnt seem to work

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • see also: https://stackoverflow.com/documentation/sed/3640/in-place-editing/12529/portable-use#t=201610180932445369797 and https://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x – Sundeep Oct 18 '16 at 09:35

1 Answers1

4

As you are editing the file in place (-i), OS X sed requires a mandatory backup filename.

You can use -i "" to get around this:

sed -i "" -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • thanks a ton. it works. removing the `-e` does not work for me though. may be because I am doing the complex sed command actually. like so: `sed -i "" -e 's/searchcriteriawith="[^"]*/searchcriteriawith="'$new_value_frmSome_var'/g' MyEditableFile.xml` – TheWaterProgrammer Oct 18 '16 at 09:57
  • I did not wish to include all this complexity in my question. hence had simplified the sed command in the sample – TheWaterProgrammer Oct 18 '16 at 09:58
  • @NelsonP No problem. I have removed that portion too. – heemayl Oct 18 '16 at 09:58