Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All, I want to extract values from top for plotting purposes. I'm currently using the following command

top -l 0 -s 1  -pid 12345 -stats rsize | awk 'NR%13==0'  

When I run this , I get the expected output. But when I try to redirect the output to a file by running the following:

top -l 0 -s 1  -pid 299 -stats rsize | awk 'NR%13==0' > output.txt  

it doesn't work. What should I do to redirect the output to a file?

I'm on a Mac OSX(Lion)

share|improve this question
It seems that batch mode (-b) is not implemented on OS X. You could set larger value for -l (say "-l 10"), but you won't be able to see the output until after the script is done, e.g. "tail -f output.txt" won't work for monitoring. – lupincho Feb 19 '12 at 7:45
@lupincho: the only problem is awk buffering. – tuergeist Feb 19 '12 at 21:32
up vote 2 down vote accepted

Your expression is rigth, but be aware that awk buffers its output. So, adding fflush(stdout) to your awk part will help.

top -l 0 -s 1  -pid 299 -stats rsize | awk 'NR%13==0; fflush(stdout)' > output.txt  
share|improve this answer
That did the trick.. Thanks tuergeist – smokinguns Feb 19 '12 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .