Date: Thu, 22 Nov 2007 19:39:42 +0200 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: ann kok <annkok2001@yahoo.com> Cc: freebsd-questions@freebsd.org Subject: Re: can you help about this script Message-ID: <20071122173942.GA3814@kobe.laptop> In-Reply-To: <874143.14419.qm@web53309.mail.re2.yahoo.com> References: <874143.14419.qm@web53309.mail.re2.yahoo.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2007-11-21 12:26, ann kok <annkok2001@yahoo.com> wrote:
> Hi all
> how command "date, hostname" run in awk program?
>
> awk -F program.awk file.txt
You don't use backticks... These are a feature of the shell, and
running a script through progname.awk is no longer a shell session.
Try system("date") in your awk(1) script:
> program.awk
>
> BEGIN { RS = "\n" ; FS = "|" }
>
> {
> print "Name:", $9
> print "Created: `date`"
> print "from: `hostname`"
> print ""
> }
BEGIN {
RS ="\n";
FS = "|";
}
{
printf "Name: %s\n", $9;
printf "Created: %s\n", system("date");
printf "From: %s\n", system("hostname");
}
Running system("hostname") once for each file may be horribly
inefficient, though. If I were you, I'd write this as a *shell* script,
which runs "hostname" once, stashes the result away in a variable, and
reuses it all the time.
Running "date" may be a bit less efficient than something like
gettimeofday(). Perl has a gettimeofday() function in the Time::HiRes
module, so it may be worth investigating if that may speed things up a
bit more.
A completely untested first try to do something like this is ...
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday);
my $hostname = `hostname`;
my $line;
while (defined($line = <STDIN>)) {
chomp $line;
my @fields = split /|/, $line;
if ($#fields >= 0) {
my ($seconds, $microseconds) = gettimeofday();
printf "Name: %s\n", $fields[8];
printf "Created: %s\n",
strftime("%Y-%m-%d %H:%M:%S", gmtime($seconds));
printf "From: %s\n", $hostname;
}
}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20071122173942.GA3814>
