3

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.

1
  • 2
    Do you want to display the entire (changed) file on the screen, or only the changed line(s)? Commented Mar 2, 2016 at 3:46

2 Answers 2

6

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.

4
  • You definitely don't need tee for this job. Commented Mar 2, 2016 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
    Commented Mar 2, 2016 at 7:03
  • @Alexander, see Stéphane's answer. Commented Mar 2, 2016 at 16:36
  • Thanks @TobySpeight, I saw it and it's a great alternative.
    – assefamaru
    Commented Mar 2, 2016 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .