Say I have this file:
cat > test.txt <<EOF
line one word
line two word
line three word
line one two word
EOF
And let's say I want to replace all of the words 'two' with 'TWO', inline in-place in the file test.txt
.
Now, what I do, is usually construct a "preview" (with -n
don't print lines, and then with /p
- print matched lines only):
$ sed -n 's/two/TWO/gp' test.txt
line TWO word
line one TWO word
... and then I usually execute the actual in-place replacement (with -i
, and without /p
):
$ sed -i 's/two/TWO/g' test.txt
$ cat test.txt
line one word
line TWO word
line three word
line one TWO word
Is there a way to get sed
to both change lines in-place in a file, and print the changed lines to stdout, from a single command line?