awk date doubt
am 18.09.2007 23:17:00 von apogeusistemas
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.
Re: awk date doubt
am 18.09.2007 23:27:53 von Stephane CHAZELAS
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 $ is th field (or the whole record if
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
Re: awk date doubt
am 19.09.2007 02:04:56 von Bill Marcum
On Tue, 18 Sep 2007 14:17:00 -0700, 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.
Re: awk date doubt
am 19.09.2007 02:46:53 von Ed Morton
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.
Re: awk date doubt
am 19.09.2007 03:04:58 von Bill Marcum
On Wed, 19 Sep 2007 00:04:56 GMT, Bill Marcum
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