For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > pls explain what action part is missing here









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 pls explain what action part is missing here
Will Ganz

2004-03-19, 8:23 pm

Platform:
C:\>ver
Microsoft Windows 2000 [Version 5.00.2195]
G:\>gawk --version
GNU Awk 3.1.0


I am learning awk for use in batch files as an quick alternative to Perl
scripts to do text searches at work. This is done on a Win32 platform and
*nix is _NOT_ available or an option due to C*O level political decisions.

I understand that awk requires a series of rules which are subdivided as a
pattern {action} for each rule. BEGIN and END are regarded as patterns and
require actions. The documentation at
http://www.gnu.org/software/gawk/ma...on-Overview.htm is
somewhat contradictory in that for an omitted action the curly braces can
be omitted as well although it is followed two lines later with an example
of empty braces as an empty action.

Due to the coding practices at work, I was putting the opening curly brace
on the line under the keywords BEGIN and END. Such as:

# this doesn't run on GnuWin32 gawk
BEGIN
{
FS = "@"
}


This was causing gawk to balk with "warning: BEGIN blocks must have an
action part". After much head banging on keyboard and doubting of sanity
for attempting to use gawk, I found that if the opening curly brace was on
the same line as the keyword BEGIN, then it would run as advertised.

# this DOES run on GnuWin32 gawk
BEGIN {
FS = "@"
}

I cannot find this documented in any of the fsf.org documentation.

My question is '¿Is this a vagarity of GnuWin32 gawk or of gawk in
general?'

TIA

Will

--
¡Microsoft delenda est!
Harlan Grove

2004-03-19, 8:23 pm

"Will Ganz" <wganz__trash_MAPS¶_@texoma.net> wrote...
....
>http://www.gnu.org/software/gawk/ma...on-Overview.htm is
>somewhat contradictory in that for an omitted action the curly braces can
>be omitted as well although it is followed two lines later with an example
>of empty braces as an empty action.


You misread the docs. From the url above:

"An omitted action is equivalent to { print $0 }:

/foo/ { } match foo, do nothing --- empty action
/foo/ match foo, print the record --- omitted action"


>Due to the coding practices at work, I was putting the opening curly brace
>on the line under the keywords BEGIN and END. Such as:

....

awk is *NOT* C. Never forget this.

>I cannot find this documented in any of the fsf.org documentation.


Really? Try

http://www.gnu.org/software/gawk/ma...ng-Started.html

which states:

"Syntactically, a rule consists of a pattern followed by an action. The
action is enclosed in curly braces to separate it from the pattern. Newlines
usually separate rules. Therefore, an awk program looks like this:

pattern { action }
pattern { action }
..."

Also, see

http://www.gnu.org/software/gawk/ma...ents-Lines.html

which states:

"Most often, each line in an awk program is a separate statement or separate
rule, like this:

awk '/12/ { print $0 }
/21/ { print $0 }' BBS-list inventory-shipped

However, gawk ignores newlines after any of the following symbols and
keywords:

, { ? : || && do else

A newline at any other point is considered the end of the statement.1"

Since patterns must be valid expressions or nothing at all, and no valid
expression could end with any of the forgoing tokens, it follows that gawk
won't ignore newlines after patterns.

Sometimes you need to connect the dots yourself.

>My question is '¿Is this a vagarity of GnuWin32 gawk or of gawk in
>general?'


Common to all awks, not just gawk, Win32 or otherwise.


Antonio Dell'elce

2004-03-19, 8:23 pm

Will Ganz wrote:

> # this doesn't run on GnuWin32 gawk
> BEGIN
> {
> FS = "@"
> }
>
>
> This was causing gawk to balk with "warning: BEGIN blocks must have an
> action part". After much head banging on keyboard and doubting of sanity
> for attempting to use gawk, I found that if the opening curly brace was on
> the same line as the keyword BEGIN, then it would run as advertised.
>
> # this DOES run on GnuWin32 gawk
> BEGIN {
> FS = "@"
> }



As far my knowledge goes this is standard for any awk:

BEGIN \
{
FS = "@"
}

and

BEGIN {
FS = "@"
}

are valid syntaxes for AWK

BEGIN
{
FS = "@"
}

is not.


Antonio

--
Antonio Dell'elce
http://www.dellelce.com/MyHome/
Ph: (IT) +39 347 6761377 (UK) +44 7816 216 963
"Timendi causa est nescire"
Will Ganz

2004-03-19, 8:23 pm

Antonio,

Thanks for the reply! Got it figured out and off to another awk philosphy
problem.

Regards,

Will

--
¡Microsoft delenda est!
TKK

2004-03-19, 8:23 pm

Antonio Dell'elce <antonio@dellelce.com> wrote in message news:<bVIXb.9011$Kc3.283665@twister2.libero.it>...
> Will Ganz wrote:
>
>
>
> As far my knowledge goes this is standard for any awk:
>
> BEGIN \
> {
> FS = "@"
> }
>
> and
>
> BEGIN {
> FS = "@"
> }
>
> are valid syntaxes for AWK
>
> BEGIN
> {
> FS = "@"
> }
>
> is not.
>
>
> Antonio


FYI --

The syntax:
BEGIN
{
FS = "@"
}


seems to work fine in TAWK.
Will Ganz

2004-03-19, 8:23 pm

> seems to work fine in TAWK.

Unfortunately, TAWK is $199 for a single license and $599 for a 5 pak.

You should have seen the conference that we had just to get it to where I
could use grep. And this is a company that has shut off all Internet access
since the COO is convinced that we got hit by the NIMDA virus from people
using Internet Explorer and not that it was due to holes in IIS. The sys
admins wanted to take out all of the floppy & CDROM drives for they were
convinced that the Klez virus was from someone bringing a music CD in.

Sigh.....

--
¡Microsoft delenda est!
Kenny McCormack

2004-03-19, 8:23 pm

In article <Xns9492C098AF823wganztexomanet@129.250.170.96>,
Will Ganz <wganz__trash_MAPS¶_@texoma.net> wrote:
>
>Unfortunately, TAWK is $199 for a single license and $599 for a 5 pak.
>
>You should have seen the conference that we had just to get it to where I
>could use grep. And this is a company that has shut off all Internet access
>since the COO is convinced that we got hit by the NIMDA virus from people
>using Internet Explorer and not that it was due to holes in IIS. The sys
>admins wanted to take out all of the floppy & CDROM drives for they were
>convinced that the Klez virus was from someone bringing a music CD in.


What any of that has to do with TAWK, I fail to see.

The game of computing is turning into a big game of "place the blame",
because of the fact that the obviously correct answer is the one that
is untenable to hold.

But, anyway, to get back to on topic, I have these comments:
1) TAWK is worth every penny.
2) The hard part today is actually finding it. As far as I can
tell, and please, someone tell me if I'm wrong, the company no
longer exists. I've tried several times over the past year or
so to contact them, to no avail.
3) The construct noted (shown below) is not legal AWK, and if TAWK
accepts it, well, that's a whatever you want to call it...

BEGIN
{
print 1+2
}

P.S. TAWK does indeed accept this (it prints 3 and exits), but gawk sez
"Warning: BEGIN blocks must have an action part", but then *fails* to read
the terminal and print 3 for every input line. I would assume that it
should, on the assumption that warnings should be non-fatal.

Will Ganz

2004-03-19, 8:23 pm

Kenny,

Apologies for belly aching about the obvious shortcomings of where I work
and not simply stating that introducing a commercial version of awk is not
a possibility.

The price list on the web site (http://www.tasoft.com/) that includes TAWK
was effective "January 1, 2003" although I have not attempted to contact
them.

Will

--
¡Microsoft delenda est!
Aharon Robbins

2004-03-19, 8:23 pm

In article <c0ugkp$g27$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
>But, anyway, to get back to on topic, I have these comments:
> 1) TAWK is worth every penny.
> 2) The hard part today is actually finding it. As far as I can
> tell, and please, someone tell me if I'm wrong, the company no
> longer exists. I've tried several times over the past year or
> so to contact them, to no avail.


Their web site is still up: http://www.tasoft.com, including phone numbers
and mailing address. I suppose worst case you could get on a plane, go to
Portland, and try to walk into their door. (:-)

> 3) The construct noted (shown below) is not legal AWK, and if TAWK
> accepts it, well, that's a whatever you want to call it...
>
>BEGIN
>{
>print 1+2
>}
>
>P.S. TAWK does indeed accept this (it prints 3 and exits), but gawk sez
>"Warning: BEGIN blocks must have an action part", but then *fails* to read
>the terminal and print 3 for every input line. I would assume that it
>should, on the assumption that warnings should be non-fatal.


I have just changed this to remove the word "warning". I used the wrong
routine to print the error message. Thanks for pointing this out.

Arnold
--
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd. arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381 Fax: +1 530 688 5518
Nof Ayalon Cell Phone: +972 51 297-545
D.N. Shimshon 99785 ISRAEL
Sponsored Links







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

Copyright 2008 codecomments.com