Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Add timestamps to Unix commands that run at intervals' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Add timestamps to Unix commands that run at intervals
Authored by: Hal Itosis on Jan 22, '10 09:06:38PM
None of the above awk or sed worked right for me (in 10.5), and as for gawk, idunno... but sharing a script and telling users to install gawk is impractical and unnecessary. Regular old awk works fine for this:

vm_stat 2 |awk '{ system("printf '%-9s' `date +%T`"); print $1,$2,$3,$4 }'
23:58:38 Mach Virtual Memory Statistics:
23:58:38 free active inac wire
23:58:38 141575 130115 9608 44445
23:58:40 141539 130188 9608 44445
23:58:42 141547 130194 9608 44445
23:58:44 141511 130200 9608 44445
23:58:46 141412 130211 9608 44445

...or, add formatting all around for better spacing:

vm_stat 2 |awk '{ system("x=`date +%T`; printf '%-9s' $x"); 
printf("%9s %9s %9s %9s\n", $1,$2,$3,$4) }'
23:58:50      Mach   Virtual    Memory Statistics:
23:58:51      free    active      inac      wire
23:58:51    141067    130589      9608     44461
23:58:52    140916    130573      9608     44589
23:58:54    141037    130561      9608     44589
23:58:56    141019    130564      9608     44589
23:58:58    140967    130568      9608     44621

-HI-

[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: slvrstn on Jan 22, '10 10:32:43PM
Getting back to a pure Perl solution, and I certainly don't want to get into a Perl golf match with Randal, but the whole substr approach is very specific to the formatting of vm_stat output, and as the OP pointed out, this changes between releases.

It can be accomplished more generally using the default autosplitting on whitespace:

vm_stat 2 | perl -ane 'printf "%.2d:%.2d:%.2d: @F[0..4]\n", (localtime())[2,1,0]'

Additional columns can be added by simply specifying more indices to the @F array, i.e., @F[0..4,6,9]

As in Hal's example, additional formatting can be added if desired:

vm_stat 2 | perl -ane 'printf "%.2d:%.2d:%.2d: %9s %9s %9s %9s\n", (localtime())[2,1,0], @F[0..4]'

This solution has the advantage over the awk (though not gawk) methods by not having to shell out to get the `date`

[ Reply to This | # ]

Add timestamps to Unix commands that run at intervals
Authored by: slvrstn on Jan 23, '10 06:15:10PM
Interesting, it previewed/looked fine under Firefox, but I see your issue under Safari. Lets hope this fixes it. My apologies.

[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: Hal Itosis on Jan 22, '10 11:18:59PM
(NOTE: this message probably appears in "code" due to a missing code tag in the previous post by slvrstn ?!)
Rats!!!... in a flurry of copy/paste editing from Terminal, i accidently posted an older version for the second example. (i.e., no need for the x= variable assignment... that part of example 2 should look just like example 1). I.e.,

vm_stat 2 |awk '{ system("printf '%-9s' `date +%T`"); 
printf("%9s %9s %9s %9s\n", $1,$2,$3,$4) }'
Edited on Jan 22, '10 11:25:07PM by Hal Itosis


[ Reply to This | # ]