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: 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 | # ]