Unix Shell - awk date doubt

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > awk date doubt





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 awk date doubt
apogeusistemas@gmail.com

2007-09-18, 7:19 pm

Hi:
Can you tell me why the date command don=B4t work in this script ?
#!/usr/bin/awk -f
$0 !~ /^ / && $0 !~ /^$/ {
time =3D $1; d=3D$("date +'%Y-%m-%d'");
print d substr($0, length(d) + 1);
}
$0 ~ /^ / {
print time substr($0, length(time) + 1);
}
$0 ~ /^Average/ { exit };

I=B4m going crazy with awk...
I=B4d like know how to be a master in awk scripts, what
is the best way to get skill in awk ???
Which book to read ?
Thank you.

Stephane CHAZELAS

2007-09-18, 7:19 pm

2007-09-18, 14:17(-07), apogeusistemas@gmail.com:
> Hi:
> Can you tell me why the date command don´t work in this script ?
> #!/usr/bin/awk -f
> $0 !~ /^ / && $0 !~ /^$/ {
> time = $1; d=$("date +'%Y-%m-%d'");
> print d substr($0, length(d) + 1);
> }
> $0 ~ /^ / {
> print time substr($0, length(time) + 1);
> }
> $0 ~ /^Average/ { exit };
>

[...]

The $(...) syntax is a shell syntax, not a awk syntax.

In awk $<whatever> is <whatever>th field (or the whole record if
<whatever> evaluates to 0.

To call a command, awk offers you several syntaxes:

system("the command"), which returns the exit status of the
command,

print "whatever" | "the command"

to start a command and feed some text to its standard input
(through a pipe).

Or

"the command" | getline someVariable

to start a command and read one line of its standard output
(through a pipe again).

In both pipe cases above, the next time you reuse that command,
it will not start a new command but instead feed or get more
output to/from the command.

In your example,

"date +%Y-%m-%d" | getline d

would store the first line of the output of that date command
into the awk variable d.

--
Stéphane
Bill Marcum

2007-09-18, 7:19 pm

On Tue, 18 Sep 2007 14:17:00 -0700, apogeusistemas@gmail.com
<apogeusistemas@gmail.com> wrote:
>
>
> Hi:
> Can you tell me why the date command don´t work in this script ?
> #!/usr/bin/awk -f
> $0 !~ /^ / && $0 !~ /^$/ {
> time = $1; d=$("date +'%Y-%m-%d'");
> print d substr($0, length(d) + 1);
> }
> $0 ~ /^ / {
> print time substr($0, length(time) + 1);
> }
> $0 ~ /^Average/ { exit };
>

$( ) works in shell, not in awk. To execute external commands and get
the output in an awk variable:
"date '+%Y-m-d'" | getline d

> I´m going crazy with awk...
> I´d like know how to be a master in awk scripts, what
> is the best way to get skill in awk ???
> Which book to read ?
> Thank you.
>


--
QOTD:
Silence is the only virtue he has left.
Ed Morton

2007-09-19, 1:28 am

apogeusistemas@gmail.com wrote:
> Hi:
> Can you tell me why the date command don´t work in this script ?


awk is not shell. If you use GNU awk you can use it's date utility
functions. If you must use shell, do it outside if you can. Check which
awk is "/usr/bin/awk" as it may be old, broken awk. If you're on Solaris
use nawk, gawk, or /usr/xpg4/bin/awk.

> #!/usr/bin/awk -f
> $0 !~ /^ / && $0 !~ /^$/ {
> time = $1; d=$("date +'%Y-%m-%d'");
> print d substr($0, length(d) + 1);
> }
> $0 ~ /^ / {
> print time substr($0, length(time) + 1);
> }
> $0 ~ /^Average/ { exit };
>
> I´m going crazy with awk...
> I´d like know how to be a master in awk scripts, what
> is the best way to get skill in awk ???


Listen to the advice you've been getting here and just spend a couple of
minutes thinking about it. You're repeatedly posting the same questions
(or variations thereof) and/or scripts with the same mistakes. To those
of us who've been responding to you, it honestly feels like you haven't
even seen any of the responses and if you have seen them, you haven't
understood them or otherwise learned from them.

> Which book to read ?


Effective Awk Programming, Third Edition By Arnold Robbins:
http://www.oreilly.com/catalog/awkprog3/

Ed.
Bill Marcum

2007-09-19, 1:28 am

On Wed, 19 Sep 2007 00:04:56 GMT, Bill Marcum
<marcumbill@bellsouth.net> wrote:
> $( ) works in shell, not in awk. To execute external commands and get
> the output in an awk variable:
> "date '+%Y-m-d'" | getline d
>

That should be '+%Y-%m-%d'


--
"You'll pay to know what you really think."
-- J. R. "Bob" Dobbs
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com