87

> can do it.

echo "text" > file

tee can do it.

echo "test" | tee file 

Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without using either > or tee?

4
  • Please try to find the answer yourself before posting questions here. The first thing we ask of our users is to search and research before posting. All you needed to do was sed '' file > newfile and you would have seen that > can indeed be used with sed just like with any other program.
    – terdon
    May 16, 2016 at 10:09
  • 3
    That's still using the > operator. I was interested if there were a sed command that could be used instead. But I'll delete this question if it's so bad.
    – Folaht
    May 16, 2016 at 10:29
  • Ah, right. OK, that's a very different question. Since you accepted the sed -i answer, I'll edit your question and clarify what you're after.
    – terdon
    May 16, 2016 at 10:34
  • I accepted "sed is not meant for data redirection as tee and > (are) meant to be".
    – Folaht
    May 16, 2016 at 10:51

3 Answers 3

123

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.

7
  • 2
    Thanks. You understood my question. So the answer is no. I thought sed on it's own maybe could do it, because it's also capable of creating backup files with the -i.bak option.
    – Folaht
    May 16, 2016 at 10:56
  • 2
    Correct. Also -i.bak is not a fixed option, you can use any extension, which you would like your backup file to have. for ex. -i.log will also work. :)
    – ankidaemon
    May 16, 2016 at 11:24
  • 1
    I just like to add that was actually looking for echo "test" | dd of=file status=none, suppressing an echo's output without using the obscure output redirection 2> /dev/null, but instead asked for sed's limitations. I was really impressed by sed's abilities as it's capable of appending lines in file, giving me the impression that sed could do anything tee could do and more.
    – Folaht
    May 16, 2016 at 11:26
  • Doesn't work with -i: sed: 1: "pubspec.yaml": extra characters at the end of p command. My command: sed -i 's/version:.*/version: 0.0.11+4/g' pubspec.yaml
    – Rebar
    Jun 3, 2021 at 22:40
  • The last option will not create the file, unfortunately
    – Ari
    Aug 31, 2021 at 3:51
14

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.

4

As ankidaemon correctly pointed out how we can save sed output to a file. I would like to add that if we are performing some operations on a file i.e replace and would like to save the output to the same file. There is a -i flag in sed which makes inplace edits possible. This however creates a backup file in the process if a suffix is provided as argument. If that's not needed, that can be done by just passing empty filename or nothing to -i flag.

Example: sed -i "s/from/to/" file is inplace changing the file.

1
  • 2
    example: sed -i '' "s/BAD/GOOD/" file.yml
    – updater
    Nov 19, 2020 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.