Re: can you help about this script

2007-11-22 Thread Giorgos Keramidas
On 2007-11-22 10:10, ann kok <[EMAIL PROTECTED]> wrote:
> Hi Giorgos
>
> Thank you
>
> But my output is from your suggstion
> printf "Created: %s\n", system("date +%Y%m%d");
>
> 20071122
> Created: 0
> 20071122
> Updated: 0
>
> how can I have output as
>
> Created: 20071122
> Updated: 20071122

You'll have to use the gsub() to strip newlines from the output of
"date"...

> In additon,
>
> ls it possible to have loop output also?
>
> I need to have
>
> print "File No:", CMA001
>
> the second record is CMA002 and then CMA003 for the
> 3rd record

Sure.  One way to do this is to print a formatted version of the special
"NR" variable of awk (NR == number of records read so far):

$ ( echo foo ; echo bar ) | awk '{ printf "%03d %s\n", NR, $0; }'
001 foo
002 bar
$

If you are going to do any amount of *serious* awk programming, I
recommend the following book:

Dale Dougherty, Arnold Robbins.  "Sed & Awk".  O'Reilly &
Associates.  2nd edition (March 1997)

http://www.oreilly.com/catalog/sed2/

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: can you help about this script

2007-11-22 Thread ann kok
Hi Giorgos

Thank you

But my output is from your suggstion 
printf "Created: %s\n", system("date +%Y%m%d");

20071122
Created: 0
20071122
Updated: 0

how can I have output as

Created: 20071122
Updated: 20071122

In additon,

ls it possible to have loop output also?

I need to have

print "File No:", CMA001 

the second record is CMA002 and then CMA003 for the
3rd record

awk -f program.awk record.txt

Thank you again






--- Giorgos Keramidas <[EMAIL PROTECTED]>
wrote:

> On 2007-11-21 12:26, ann kok <[EMAIL PROTECTED]>
> 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;
> }
> }
> 
> 

--- Giorgos Keramidas <[EMAIL PROTECTED]>
wrote:

> On 2007-11-21 12:26, ann kok <[EMAIL PROTECTED]>
> 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;
> }
> }
> 
> 



  

Be a better pen pal. 
Text or chat with friends inside Yahoo! Mail. See how.  
htt

Re: can you help about this script

2007-11-22 Thread Giorgos Keramidas
On 2007-11-21 12:26, ann kok <[EMAIL PROTECTED]> 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;
}
}

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


can you help about this script

2007-11-21 Thread ann kok
Hi all

how command "date, hostname" run in awk program?

awk -F program.awk file.txt

program.awk

 BEGIN { RS = "\n" ; FS = "|" }

 {
   print "Name:", $9
   print "Created: `date`"   
   print "from: `hostname`"
   print ""
 }

Thank you


  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"