I have a linux command based on top that outputs my current tasks snapshot (I’ve assembled it from various SE topics so it may not be optimal but it works for me):
top -bn 1 -i | grep "^ " | awk '{ printf("%s%s%sn"," {CPU:"$9",","MEM:"$10",","CMD:"$12"}"); }' | tail -n +2 | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
The output is something like this:
[2018-11-20 18:09:11] {CPU:0.0,MEM:0.2,CMD:uwsgi}
[2018-11-20 18:09:11] {CPU:0.0,MEM:0.0,CMD:uwsgi}
[2018-11-20 18:09:11] {CPU:0.0,MEM:0.0,CMD:nginx}
[2018-11-20 18:09:11] {CPU:0.0,MEM:0.0,CMD:nginx}
[2018-11-20 18:09:11] {CPU:0.0,MEM:0.0,CMD:nginx}
Actually, I get like 300 lines for every execution of my command. I would like to remove the lines that have “CPU:0.0,MEM:0.0”.
I’ve tried: top -i
but that removes all “idle” tasks, which means “CPU:0.0” – however, that way, I am losing all the tasks like: CPU:0.0,MEM:0.2 (which I want to keep)
Perhaps add an if-then-else somehow inside the awk part of the command? I’ve tried to hack it but it just doesn’t work.
Advertisement
Answer
grep will do:
... | grep -v "CPU:0.0,MEM:0.0"
From the man page:
-v, --invert-match Invert the sense of matching, to select non-matching lines.