For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2006 > help with embedding in awk please









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help with embedding in awk please
Mike

2006-03-10, 6:55 pm

I have the following problem, how can I use the date function, assign it to
a variable and then print it back out
( it is supposed to take the first field out of the testdates.cvs file (firs
field is 04/04/2006 ) and print out the day for that as in
Tuesday 04/04/2006

awk 'BEGIN
{theday=`date +%A -d $1`}
END {print theday , $1}' testdates.csv
does awk allow this?
Cheers Mike..
Janis Papanagnou

2006-03-10, 6:55 pm

Mike wrote:
> I have the following problem, how can I use the date function, assign it to
> a variable and then print it back out
> ( it is supposed to take the first field out of the testdates.cvs file (firs
> field is 04/04/2006 ) and print out the day for that as in
> Tuesday 04/04/2006
>
> awk 'BEGIN
> {theday=`date +%A -d $1`}
> END {print theday , $1}' testdates.csv


This won't work; you are mixing shell and awk syntax.

> does awk allow this?
> Cheers Mike..


Depends on the awk you have. GNU awk has date functions; you may use the
function split() to get three array elements and construct a string that
you feed into gawk's mktime() function, and to obtain current time use
systime(). In standard awk's you may either pass the output of the date
command fro the shell through a variable to awk...

awk -v datevar=`date ...` 'BEGIN { ...use datevar here... }'

or access the date command using getline

awk 'BEGIN { "date ..." | getline datevar ; ...use datevar here... }'


Janis
Patrick TJ McPhee

2006-03-11, 6:56 pm

In article <HniQf.33707$wl.26451@text.news.blueyonder.co.uk>,
Mike <austinsoft@blueyonder.co.uk> wrote:

% awk 'BEGIN
% {theday=`date +%A -d $1`}
% END {print theday , $1}' testdates.csv
% does awk allow this?

You can pipe the output of any shell command into a varible using getline
and the pipe operator

BEGIN { "date +%A -d " ARGV[1] | getline theday }

Note, though, that you have to explicitly tell the process to go away
by calling close with the full command as an argument

BEGIN { cmd = "date +%A -d " ARGV[1]
cmd | getline theday
close(cmd)
}

If you want the shell's $1, you should assign it to a variable on the
awk command line.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Mike

2006-03-12, 6:55 pm

Patrick TJ McPhee wrote:

> In article <HniQf.33707$wl.26451@text.news.blueyonder.co.uk>,
> Mike <austinsoft@blueyonder.co.uk> wrote:
>
> % awk 'BEGIN
> % {theday=`date +%A -d $1`}
> % END {print theday , $1}' testdates.csv
> % does awk allow this?
>
> You can pipe the output of any shell command into a varible using getline
> and the pipe operator
>
> BEGIN { "date +%A -d " ARGV[1] | getline theday }
>
> Note, though, that you have to explicitly tell the process to go away
> by calling close with the full command as an argument
>
> BEGIN { cmd = "date +%A -d " ARGV[1]
> cmd | getline theday
> close(cmd)
> }
>
> If you want the shell's $1, you should assign it to a variable on the
> awk command line.

Many thanks ;-)
Mike..
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com