I issued the following command, thinking it would take the contents of foo.txt
, process it, and write it back to the original file. Instead when I opened the file, it was empty.
cat foo.txt | tr " " "\n" > foo.txt
What exactly is going on in this line, and how can I rephrase it to redirect output back to foo.txt
?
tr
ever sees the file. You might trytmp=$(mktemp); tr " " "\n" < foo.txt; mv -f $tmp foo.txt; rm -f $tmp
– doneal24 Mar 24 '22 at 18:14sponge
from the moreutils package:tr " " "\n" < foo.txt | sponge foo.txt
– glenn jackman Mar 24 '22 at 21:00&& mv
instead of; mv
-- this way, the original file does not get overwritten if the process fails; 2) $tmp no longer exists torm
, it has already been moved. – glenn jackman Mar 24 '22 at 21:02echo hello hello hello > foo
cat < foo >> foo
– Philip Couling Apr 13 '22 at 13:55