How to Save sed Output Directly to a File
You have 3 options here:
I needed this when doing bulk find-and-replace across config files in a deployment script. The -i option is the most common approach since it edits the file in place, but the tee and redirect methods are useful when you want to preview the output or write to a different file.
Option 1: use tee with sed
sed 's/Hello/Hi/g' file-name | tee file
Option 2: use > with sed
sed 's/Hello/Hi/g' file-name > file
Option 3: use sed with -i option
sed -i 's/Hello/Hi/g' file-name
Note that on macOS, sed -i requires an empty string argument like sed -i '' 's/Hello/Hi/g' file-name — without it you’ll get an “invalid command code” error. The GNU version on Linux doesn’t need this. If you’re writing cross-platform scripts, the tee approach avoids this compatibility headache.