unixsedsolaris

sed edit file in-place


How do I edit a file in a single sed command? Currently, I have to manually stream the edited content into a new file and then rename the new file to the original file name.

I tried sed -i, but my Solaris system said that -i is an illegal option. Is there a different way?


Solution

  • The -i option streams the edited content into a new file and then renames it behind the scenes, anyway.

    Example:

    sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
    

    while on macOS you need:

    sed -i '' 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename