0

I'm using this command

sed -i'' -e "s/MARKETING_VERSION = 0.0.0;/MARKETING_VERSION = ${version//v};/" -f ios/App/App.xcodeproj/project.pbxproj

to replace the text in that file. This works on Ubuntu, but on Mac it changes the file and also creates a new file in the same directory called "project.bxproj-e" which seems to be the original file.

This command works on Mac:

sed -i '' "s/MARKETING_VERSION = 0.0.0;/MARKETING_VERSION = ${version//v};/" -f ios/App/App.xcodeproj/project.pbxproj

For it to work, I had to add a space after -i and remove the -e, however on Ubuntu I get this error:

sed: can't read s/MARKETING_VERSION = 3.0.0;/MARKETING_VERSION = 0.0.0;/: No such file or directory

I'm just looking for something that works on both envs.

If it helps, sed --version gets me "sed (GNU sed) 4.4" on Ubuntu. On Mac sed --version throws an error, but I can get this from the man page: "The sed utility is expected to be a superset of the IEEE Std 1003.2 (``POSIX.2'') specification." and also mentions that "-E -I -a and -i are non-standard and may not be available on other operating systems". But I don't know how to do it without them.

byronaltice
  • 623
  • 6
  • 18
  • Stop using `-i`. The rest is compatible -- `-e` and `-E` are both fine. – Charles Duffy Jun 28 '21 at 18:55
  • All `sed -i` does is tell sed to write output to a temporary file, then rename that temporary file over the input file. You can of course do that same thing in your own code; `tempfile=$(mktemp "$infile".XXXXXX) || exit; sed ... <"$infile" >"$tempfile" && mv -- "$tempfile" "$infile"` and you're there for the easy cases. – Charles Duffy Jun 28 '21 at 18:56
  • ...you really don't need to remove `-e` for MacOS support. `sed -e` is fine in upstream BSDs, and that's still true in MacOS; see https://ss64.com/osx/sed.html – Charles Duffy Jun 28 '21 at 18:59
  • @CharlesDuffy shouldnt' I be able to redirect the output of sed to the same file, instead of making a new file? When i do that, I just end up with a blank file. e.g.: sed "s/with/without/g" test > test – byronaltice Jun 28 '21 at 19:55
  • No, you shouldn't be able to do a truncating redirect to your input file (and still read preexisting input from it). Keep in mind that redirections are set up _before the program starts_, so `sed infile` has opened `infile` _for output_ with the `O_TRUNC` flag causing its contents to be deleted _before `sed` has started to run_, and thus before it was able to read any input. – Charles Duffy Jun 28 '21 at 20:03
  • @CharlesDuffy Thanks, this is working for me. Would you want to post an answer so I can give you credit? – byronaltice Jun 28 '21 at 22:24
  • The question is already asked elsewhere on the site, so it's more appropriate for it to be closed with a link to the canonical instance than to have a new answer that's necessarily less polished (or at least, which has been seen by fewer eyes, and thus subject to less review and editing). – Charles Duffy Jun 28 '21 at 22:33

0 Answers0