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 me say that I want to replace all the words "two" with "TWO", inline in place in the file test.txt .
Now, what I do is usually create a "preview" (with -n do not print lines, and then /p print only matched lines):
$ sed -n 's/two/TWO/gp' test.txt line TWO word line one TWO word
... and then I usually do 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 both to change lines in place in a file, and to print changed lines in stdout from one command line?
source share