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: joem on Jan 22, '10 08:54:43AM
Or you could use the following, as it's easier to understand what's going on:
vm_stat 2 | gawk '{print strftime("%H:%M:%S:"), $1, $2, $3, $4}'

(note: this requires the GNU version of awk installed.)

[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: joem on Jan 22, '10 08:58:27AM

Sorry, I should have mentioned that I wrote that on Leopard (in case it matters), and that you can pick and choose what columns you want by knowing that $1 is the first column, $2 is the second, and so on....



[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: ctierney on Jan 22, '10 10:27:31AM
Hello,
If you don't have gawk, then you can try the following workaround:
vm_stat 2 | awk -v d=`date +%T:` '{print d, $1, $2, $3, $4}'


[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: kps on Jan 22, '10 11:07:16AM
That doesn't do the right thing; it prefixes every line with the time the command was started. Try
awk '{system("echo -n \"`date +%T` \""); print $0}'
Or using just the shell:
while read x; do echo "$(date +%T) $x"; done
More likely you'd make one of these a script, shell function, or alias, so that you could apply it easily to any output.

[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: ctierney on Jan 23, '10 04:24:45AM

Oh! You're right. I was paying attention to my test output. Thanks.



[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: Anonymous on Jan 22, '10 04:29:27PM
Retain the formatting with cut:
vm_stat 2 | cut -36 | sed "s/^/$(date --utc +%Y-%m-%dT%H:%M:%SZ:) /"
My usage of date also gives you UTC ISO timestamping for free...

[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: mael on Jan 23, '10 04:56:26PM

hm...



~ $ vm_stat 2 | cut -36 | sed "s/^/$(date --utc +%Y-%m-%dT%H:%M:%SZ:) /"
cut: illegal option -- 3
usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]
date: illegal option -- -
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
w



[ Reply to This | # ]
Add timestamps to Unix commands that run at intervals
Authored by: Soliman on Jan 27, '10 05:26:23AM
Then just use cut -c 36 ;)

I still prefer the perl/awk version because fixing width at 36 is BADİ

---
Sylvain


[ Reply to This | # ]