[Linux] gawk stuff
Danny Rathjens
linux@flux.org
Fri, 18 May 2007 15:04:11 -0400
I use perl's DateTime module for most date stuff nowadays since it understands timezones, leap seconds, etc. :)
e.g.
#grab oldest live apache logs before they get rotated next day and add proper datestamp to filename
for log in cc.access proxy.access cc.error gemini.error proxy.error; do
ssh lain "nice -19 gzip --to-stdout /var/log/httpd/$log.7" >/data/backups/lain-logs/$log.`/usr/bin/perl -MDateTime -e'print DateTime->now->set_time_zone("US/Eastern")->subtract(days=>7)->ymd'`.gz
done
Incidentally, don't you need to escape that * in your scp command since you want it to
expand remotely instead of locally?
Kwan Lowe wrote:
> While troubleshooting a problem at work I had to make a script that grabs some log
> information from a remote server. There's probably a simpler way to do this, but
> this is what I came up with:
>
> #!/bin/sh
>
> YESTERDAY=`date +%s | awk '{printf "%s%s\n", strftime("%m",($0-86400)),
> strftime("%d",($0-86400) );}'`
> REPORT_DIR=/var/www/html/nmon_all
>
> echo Copying reports to Report Web Page
> for SERVER in vm-web-01 vm-web-02 vm-web-03; do
> # Copy only the previous day
> scp -rp vm-loghost:reports/${SERVER}/*${YESTERDAY}.nmon
> ${REPORT_DIR}/${SERVER}/
> done
>
> No point to this post except that I thought the date/time handling stuff of gawk was
> pretty cool. Some years ago I saw a similar script on a Solaris machine... Except
> that script required some pretty heavy lifting to check the day of the week, month,
> etc. and do subtractions based on it. With all the DST stuff recently, I doubt that
> those old scripts would continue to work.
>
>
>