When running the command sed -e “s/a/b/” /tmp/file >/tmp/file While /tmp/file contains data,
why is /tmp/file empty afterwards?
A.
The file order is incorrect. The destination file must be mentioned before the command to
ensure redirection.
B.
The command sed did not match anything in that file therefore the output is empty.
C.
When the shell establishes the redirection it overwrites the target file before the
redirected command starts and opens it for reading.
D.
Redirection for shell commands do not work using the > character. It only works using
the | character instead.
Explanation:
Test it out.
If you
cd ~
touch test.txt
echo “abababababababab” > test.txt
sed -e “s/a/b/” test.txt > test.txt
cat test.txt
bbbbbbbbbbbbbbbb
not empty because it matched something. Meaning C is incorrect. If C was correct then it would still be blank even though there was a match.
now if you do
touch test2.txt
echo “cfcfcfcfcfcfcfcfcf” > test2.txt
sed -e “s/a/b/” test2.txt > test2.txt
cat test2.txt
it comes up empty because it did not match anything. This, of course, is ONLY if you are redirecting into the same file you are reading from. If you redirect into a new file then it will not show up blank even though it did not match anything.
0
1