duyne

    Tip - Fix Sed I Issue On Mac Os

    • sed
    • macos
    • tip

    Small world. Big idea!

    The Problem

    When I working with sed to write a file script for work. I get an error when I use sed -i to replace a word in a script file but I get an error.

    I ran the command:

    sed -i 's/pattern/replacement/g' file.sh
    # or
    sed -i 's/pattern/$replacement/g' file.sh # $replacement is from the environment

    An Error occurred:

    sed: 1: "file.sh": unterminated substitute in regular expression

    This sed version was May 10, 2005 as I saw in man sed.

    Fix It

    Notes:

    In sed there are 2 options that can easily be confused with -i and -e options.

    • -i: This flag tells sed to edit the file in-place, i.e., modify the file directly.
    • -e: The -e option in sed is used to specify multiple editing commands or scripts within a single sed invocation. It allows you to apply multiple transformations to the input text.

    Download the newest version of GNU sed from here:

    π ~/opt/ ❯ tar xfz sed-4.9.tar.gz && cd sed-4.9
    π ~/opt/ ❯ ./configure
    π ~/opt/ ❯ make
    π ~/opt/ ❯ sudo make install