sed command with -i option failing on Mac, but works on Linux

前端 未结 12 2108
情歌与酒
情歌与酒 2020-11-22 05:04

I\'ve successfully used the following sed command to search/replace text in Linux:

sed -i \'s/old_link/new_link/g\' *

However,

12条回答
  •  忘了有多久
    2020-11-22 06:10

    If you use the -i option you need to provide an extension for your backups.

    If you have:

    File1.txt
    File2.cfg
    

    The command (note the lack of space between -i and '' and the -e to make it work on new versions of Mac and on GNU):

    sed -i'.original' -e 's/old_link/new_link/g' *
    

    Create 2 backup files like:

    File1.txt.original
    File2.cfg.original
    

    There is no portable way to avoid making backup files because it is impossible to find a mix of sed commands that works on all cases:

    • sed -i -e ... - does not work on OS X as it creates -e backups
    • sed -i'' -e ... - does not work on OS X 10.6 but works on 10.9+
    • sed -i '' -e ... - not working on GNU

    Note Given that there isn't a sed command working on all platforms, you can try to use another command to achieve the same result.

    E.g., perl -i -pe's/old_link/new_link/g' *

提交回复
热议问题