-1

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%s\n","
{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 -ibut 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.

DraxDomax
  • 1,008
  • 1
  • 9
  • 28
  • Could you please post output of your `top` command itself we may do it in single `awk` or `sed` itself too. And could save many sub processes to be created too. – RavinderSingh13 Nov 20 '18 at 18:33
  • add the condition `$9+$10` before the statement, which will filter out the records where both are zero. e.g. `awk '$9+$10{printf ...` – karakfa Nov 20 '18 at 19:11

1 Answers1

1

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.
steffen
  • 16,138
  • 4
  • 42
  • 81
  • we live in such a world where 2 days is enough to write some code and test that it works normally ;) Anyway, thanks for your help, it's a simple fix that gives me confidence that it'll work fine! – DraxDomax Nov 22 '18 at 22:15