|
|
Add timestamps to Unix commands that run at intervals
Some of the above scripts do not work for me, and I'm not sure why you need awk or perl at all.
This simple script also strips the vm_stat headers and formats the output.
Add timestamps to Unix commands that run at intervals
@ bashbash:
Add timestamps to Unix commands that run at intervals
Many apologies, I should have explained my script.
If you don't want to filter out the headers, don't apply the filter:
If you want to retain the original formatting, pop in some quotes:
If you want to use positional parameters and printfs use something like this:
That one mimics the previous awk script.
I thought the OP wanted something that was easily machine readable. In that case maybe something like this would do the trick:
In all cases, bash has plenty of builtin formatting without resorting to language soup :) I have a great fondness for other languages (especially awk), but let's not perpetuate the myth that bash cannot do these things just as easily. There is also an overhead of invoking extra interpreters.
Add timestamps to Unix commands that run at intervals
OK, let's look at this extra overhead issue, because it's exactly what I was talking about in my original post when I mentioned the advantage of not shelling out to get the timestamp.
To benchmark, we want to generate a lot of data quickly, so I used time od -N 1000000 -D /dev/random | wc 62501 312501 4625008 real 0m0.310s user 0m0.170s sys 0m0.153s to generate around 62k lines of the form 0000000 455208253 2432249641 1505621869 3979473917 0000020 2293418554 1733714563 3308048974 965145382 0000040 2395066143 3983717352 2757720288 3811753968 ... You see that it takes less than half a second to generate the data. Then, comparing methodologies: Perl time od -N 1000000 -D /dev/random | perl -ane 'printf "%.2d:%.2d:%.2d: @F[0..4]\n", (localtime())[2,1,0]' > /dev/null real 0m0.869s user 0m0.813s sys 0m0.162s bash time od -N 1000000 -D /dev/random | while read a b c d e;do printf "%s %9s %9s %9s %9s %9s\n" $(date +%T) $a $b $c $d;done > /dev/null real 3m43.331s user 0m52.922s sys 2m27.505s So, it takes less than a second with the pure Perl and over 3 1/2 minutes in pure bash. Any approach that has to invoke a new command to get the timestamp each time through the loop is going to suffer by the same comparison.
Add timestamps to Unix commands that run at intervals
Holy Moly Perl-man... point well made, and taken. 8-)
How is it that Perl manages to get the current timestamp for each iteration so much _more_ quickly? Doesn't it need to request that info (in real time) from the OS as well? [it also seems to parse the data strings faster too] Anyway, thanks for that impressive proof.
Add timestamps to Unix commands that run at intervals
In the end, both perl and the `date` command are making the same system library call, time.
Add timestamps to Unix commands that run at intervals
Wow! |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.09 seconds |
|