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

后端 未结 13 2262
心在旅途
心在旅途 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条回答
  •  萌比男神i
    2020-11-22 08:32

    This works with GNU sed, but not on OS X:

    sed -i -e 's/foo/bar/' target.file
    sed -i'' -e 's/foo/bar/' target.file
    

    This works on OS X, but not with GNU sed:

    sed -i '' -e 's/foo/bar/' target.file
    

    On OS X you

    • can't use sed -i -e since the extension of the backup file would be set to -e
    • can't use sed -i'' -e for the same reasons—it needs a space between -i and ''.

提交回复
热议问题