Can sed save its output to a file?

tee and > can be used for data redirection because these are meant to be used for data redirection in linux.

sed on the other hand is a stream editor. sed is not meant for data redirection as tee and > meant to be. However you can use conjunction of commands to do that.

use tee or > with sed

sed 's/Hello/Hi/g' file-name | tee file

or

sed 's/Hello/Hi/g' file-name > file

use sed with -i option

sed -i 's/Hello/Hi/g' file-name

the last one does not redirect, instead it will make changes in the file itself.


sed has the w command that might do what you require:

w filename

Write the current pattern space to filename.

sed 'w file' on its own will have the same effect as tee file. If there are other sed commands, put the w last:

sed 's/This/That/;w file'

However, this won't be affected by the -n/--quiet/--silent option. That only suppresses the content that would otherwise have gone to standard output.