You will find over the long run that this behavior is not consistent.
The problem is, that tee
at some point in time opens the file for writing. From that moment on, the file is truncated. That is independent of which program reads the file.
It just happens that cat
is very quick and that the reading of the file apparently has been finished before tee
opens the file. But if for example the system load is bigger, cat
may not have been finished, and the pipeline with cat
may also truncate.
So, do not write to the same file as you are reading your pipeline from. Instead use Gnu awk
's -i inplace
, or if that is not available on your system, use
cp file file.tmp &&
awk '...' file.tmp | tee file
head -1 file | tee -a file
with utilscat
,head
,tail
cat
ing the file avoids truncation? If you havemore-utils
package installed, why do you think someone wrotesponge
? If the file is small, do you expectawk '...' file | { sleep 2 ; tee file}
to work any better?more-utils
installed. So I usetee
asawk
cannot do inplace operation likesed
's-i
awk
then save it.