From owner-freebsd-questions@FreeBSD.ORG Thu Nov 22 17:41:02 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8898D16A41B for ; Thu, 22 Nov 2007 17:41:02 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id CFFC213C4D5 for ; Thu, 22 Nov 2007 17:41:01 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (vader.bytemobile-rio.ondsl.gr [83.235.57.37]) (authenticated bits=128) by igloo.linux.gr (8.14.1/8.14.1/Debian-9) with ESMTP id lAMHdmk5025984 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 22 Nov 2007 19:40:01 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.2/8.14.2) with ESMTP id lAMHdh6P003974; Thu, 22 Nov 2007 19:39:43 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.2/8.14.2/Submit) id lAMHdhZE003973; Thu, 22 Nov 2007 19:39:43 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Date: Thu, 22 Nov 2007 19:39:42 +0200 From: Giorgos Keramidas To: ann kok Message-ID: <20071122173942.GA3814@kobe.laptop> References: <874143.14419.qm@web53309.mail.re2.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <874143.14419.qm@web53309.mail.re2.yahoo.com> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.999, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.40, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: can you help about this script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Nov 2007 17:41:02 -0000 On 2007-11-21 12:26, ann kok 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 = )) { 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; } }