Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I'm trying to write a nice csv-file based on some output from top. I reformat the output with awk like this:

top -b | nawk '/Cpu/ || /Tasks/ { if($1 ~ /Cpu/) { printf "%s,",$3 } else { printf "\n" } }'

That works perfectly. Now I want to save the output to a file. I would think that using > output.log should work:

top -b | nawk '/Cpu/ || /Tasks/ { if($1 ~ /Cpu/) { printf "%s,",$3 } else { printf "\n" } }' > output.log

However, this simply results in an empty file when I Ctrl-C out of the top-process. What am I doing wrong?

share|improve this question

Unix’s standard I/O (“stdio”) library detects what kind of thing (data sink) it is writing to.  If it detects a terminal (i.e., a window), it writes data immediately when the program requests it.  When writing to a file, however, the I/O library buffers data and writes it in blocks of 512 (or more) bytes at a time.  Of course it flushes the buffer (writing out a partial block) when the calling program exits — if it exits cleanly.  An abnormal termination (as caused by Ctrl+C) can leave you with an incomplete output file.

To fix, try:

top -b | ( trap "" 2; nawk '(your nawk command)' > output.log)`

The trap command will make the nawk command immune to the Ctrl+C.  (Of course the Ctrl+C will still kill the top process, and nawk will terminate (cleanly!) when it gets an end of file (EOF) on the pipe.

P.S. This assumes (requires) that you are running bash or a bash-compatible shell.

share|improve this answer

Top keeps running. You want to run it a limited amount of times. Use the -n option. like so:

top -b -n 1 | awk '{ ... }'

Alternatively, you can quit top when you feel like doing so with C-\. Using C-c will abort it and break the pipe prematurely (before awk gets anything).

$ top -b | cat -n > moo ; wc -l moo
352 moo
share|improve this answer
First of all, I believe that you mean Ctrl-\, not Ctrl-/. Secondly, does that really change anything? By not piping top’s output through awk, you are getting more output more quickly. Do you really end up getting 352½ lines, with the 353rd cut off in the middle, at a file size that is a multiple of 512? – Scott Nov 8 '12 at 18:17
You get the last "good" line and all lines before that. No half lines (usually - but that depends on how nawk is treating output and input ). Actually, I think I misunderstood your question. The problem is probably that nawk doesn't write anything when top is interrupted like that. It works as you expect it to here though. It may be relevant to include your versions of tbeing hephe shell, nawk and OS. Sorry I could not help. – Ярослав Рахматуллин Nov 8 '12 at 18:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.