I'm using

top -l 0 -s 1  -pid XXX -stats rsize | awk 'NR%13==0; fflush(stdout)' > output.txt  

to store the rsize value of a process in a file. Currently the contents of output.txt look like this:


I would like to modify this one-liner to include timestamp along with the value. How do I modify the awk part of the above one-liner and add timestamp to it? The resultant output should look like this:

1338936728 3828K+  
1338936729 3838K

I'm on a Mac OSX. I would like to make a timestamp Vs rsize plot.

  • Can you include a complete output line? I don't have an OSX machine, and my version of top doesn't take the same flags – Michael Mrozek Jun 5 '12 at 23:02
  • @MichaelMrozek: What do you mean by "complete output line"? I'm essentially running the top command in batch mode in the above one-liner – smokinguns Jun 5 '12 at 23:05
  • A line unprocessed by awk. I assume "timestamp" is one of the other columns – Michael Mrozek Jun 5 '12 at 23:16
  • @MichaelMrozek: There is no timestamp column in the top command output. I thought of modifying the awk part of the one-liner to add a timestamp – smokinguns Jun 5 '12 at 23:28
  • I am on a mac and that's not giving me anything remotely like your output. – Kevin Jun 6 '12 at 1:30

It depends on your awk version (you probably have to use gawk instead of awk) or directly run date in your awk commandline, but

awk 'NR%13==0 { printf  "%d %s\n",  systime(), $0 ; fflush(stdout) }'

will print the current timestamp in front of the current line. For more information have a look at the gawk documentation about time functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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