Forget about watch, this should do, at least theoretically:
while true; do # I cut the header first with tail cat file <(ps aux | tail -n +2 | sort -rk 3,3 | head -n 2) | sort | uniq | sponge file # A certain amount of delay sleep 0.1 done
The only problem is that since CPU usage of each process varies over time, and what you are seeking for is particularly the CPU usage, the effect of uniq
is very limited and still a large amount of data is produced. You can use cut
to suppress the variables that are not important for you, so that their variation does not increase the number of data points produced.