sed in-place flag that works both on Mac (BSD) and Linux

后端 未结 13 2264
心在旅途
心在旅途 2020-11-22 08:18

Is there an invocation of sed todo in-place editing without backups that works both on Linux and Mac? While the BSD sed shipped with OS X seems to

13条回答
  •  滥情空心
    2020-11-22 08:36

    As Noufal Ibrahim asks, why can't you use Perl? Any Mac will have Perl, and there are very few Linux or BSD distributions that don't include some version of Perl in the base system. One of the only environments that might actually lack Perl would be BusyBox (which works like GNU/Linux for -i, except that no backup extension can be specified).

    As ismail recommends,

    Since perl is available everywhere I just do perl -pi -e s,foo,bar,g target.file

    and this seems like a better solution in almost any case than scripts, aliases, or other workarounds to deal with the fundamental incompatibility of sed -i between GNU/Linux and BSD/Mac.

提交回复
热议问题