1

I have got strings stored in two variables (line is current line and new is the replacement). My code looks like this :

sed -i "s@line@new@" output_file

However, this solution does not work on FreeBSD.

Is there any way to modify this code just a bit so it would work?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
lowcase_m
  • 25
  • 7

1 Answers1

4

Yes. Use:

sed -i '' "s@line@new@" output_file

On BSD systems (and macOS too), the sed command's -i option requires a suffix, which may be attached to the -i or a separate argument. However, when the suffix is empty, it must be a separate argument. This is different from GNU sed, where the -i option takes an optional suffix, but if specified, it must be attached to the -i option. Scripts portable between the two (BSD and GNU) therefore must be written with an explicit non-empty suffix attached to the -i option. Note that such scripts may still be unportable to other POSIX systems; the -i option is not standardized (as you can tell from the divergent behaviour).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278