1

I need to change replace a text in a file and output the whole text file with the changed text and also print it to the screen.

This is what I got:

sed 's/123/456/g' number > number123

This changes the text and outputs to the new file but it won't print to the terminal.

How do I get it so it prints the changed text file to the terminal in that one command.

Any help is appreciated.

  • 2
    Do you want to display the entire (changed) file on the screen, or only the changed line(s)? – G-Man Mar 2 '16 at 3:46
4

You can simply do the following:

sed 's/123/456/g' number | tee number123

The command tee reads from standard input and writes to standard output and files.

  • You definitely don't need tee for this job. – don_crissti Mar 2 '16 at 6:22
  • @don_crissti how would you redirect stdout to a file and still have it printed on screen without using tee? I would be interested to see an alternative. – assefamaru Mar 2 '16 at 7:03
  • @Alexander, see Stéphane's answer. – Toby Speight Mar 2 '16 at 16:36
  • Thanks @TobySpeight, I saw it and it's a great alternative. – assefamaru Mar 2 '16 at 16:53
2
sed 's/123/456/g; w number123' number

Note that it prints (and writes in number123) all the lines, including those that didn't contain 123.

To only print the modified ones:

sed -e 's/123/456/gw number123' -e t -e d number

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.