no data redirected to a file with top and grep - why?

HI all,

I want to capture cpu data in batch mode of "top" command and redirect to a file like this:

top -b > cpu.dat

it works!
But I want to capture only Cpu lines, so i have:

top -b | grep ^Cpu >cpu.dat

Then I got an empty output file.
Why?
Could somebody explain and help me to make it work.
Thanks

Hard to explain if we do not know input data.
Anyway - 'grep ^Cpu' should work. Empty output file tells only that there are no lines with word "Cpu" on the beggining of a line.

hi,
the direct answer for your 'why' is
->the output is not going to std out it is going to std err

secondly how to accomplish it
->method 1
$top 2>outputOfTop
or
$top >& outputOfTop

then
$grep pattern outputOfTop

->method 2
open two terminal term1 and term2

in term1
$mkfifo pipe
$top 2>pipe

in term2
$grep pattern pipe

make sure the working directory is same in both the terminal
i hope this should work

Best Regards,
Rakesh UV

uvrakesh: if "top -b >cpu.dat" worked then the output was not being written to stderr.

My copy of top (Ubuntu Linux 7.10) writes to stdout. But it keeps on looping even with the -b option, so I don't have exactly the same version.

fongthai: if top -b >cpu.dat works then does grep ^Cpu cpu.dat work? That would be a workaround at least.

top -b | grep ^Cpu output is sure no error, it output should be like this:

"Cpu(s): 1.7%us, 0.8%sy, 0.0%ni, 96.9%id, 0.5%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 0.0%us, 1.3%sy, 0.0%ni, 98.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st"

i should go to stdout. I think.

I tried your methods but they don't work.
Any other idea?

Repeat: do you get these lines in the file when you do top -b >cpu.dat?

Yes, I got them.
I don't want to get data first then using grep, because it make output file become a huge file. I want to run top -b for one or two days, that's the issue.

If you run it without redirection, do you see the results on your screen?

If you terminate it with ctrl-c, the output might still be buffered somewhere (weak hunch, might be wrong) -- if you let it run for long enough, does the output become visible?

era,

top -b will run looping, of course. I will use, for example: "top -b -d 10" it will refresh every 10 seconds.

Tested here; looks like I'm right. If I hit ctrl-c after five seconds (enough to produce about two matching lines, I guess) the output file is empty. If I run it for a minute or so, I still don't get output in the file. Then all of a sudden, lots of output. It's output buffering at play.

If you have grep --line-buffered, try with that, just to see that it works. If you run it for long enough, you probably want to take it out again, though.

@era: Yes, I can see the result on the screen without redirection.
I left it run for half a day (without grep), and kill it, I got the output with over 200MB

era, have you got any solution for it?

Scroll back and read?

You answer:

But I still keep the idea of using grep then redirect to file.

My feedback:

The problem is that grep does not write the result immediately. If you quit it before the first result has been written, the output file will still be empty. Run it without caching if you have a grep which has that option, or just live with the fact that the last buffer (probably something like 4k to 8k) might be lost when you quit it.

Did you miss comment #10 or did you not understand it?

OKie, got it work with --line-buffered option in grep.
Sorry, I was not carefully read it.

a million thanks!