undocumented sequence 
Author Message
 undocumented sequence

Hi all. I just discovered the following program seems to be valid for
at least mawk and gawk, and I can't stand why:

awk '{print} foobar'

I thought the only valid construction was something like

        pattern {action}

Is this a completely different and magic feature ? :)

--
BBP



Fri, 02 Jan 2004 16:48:15 GMT  
 undocumented sequence

Quote:
> Hi all. I just discovered the following program seems to be valid for
> at least mawk and gawk, and I can't stand why:

> awk '{print} foobar'

> I thought the only valid construction was something like

> pattern {action}

> Is this a completely different and magic feature ? :)

> --
> BBP

Well, if (foobar == ""), as it does, then this is the same as:

awk '{print} '

what would you expect this to do?

Peter
--


Opinions expressed are my own and not necessarily those of my employer



Fri, 02 Jan 2004 19:49:21 GMT  
 undocumented sequence

Quote:
> Well, if (foobar == ""), as it does, then this is the same as:

> awk '{print} '

> what would you expect this to do?

Nothing particular, I was just wondering how this may be correct.
Because from the mawk manual page, I get:

"An AWK program is a sequence of pattern {action} pairs and user
function definitions.". And I did not know variables are expanded even
out of any pattern or action block.

Thnaks for your answer :)

--
BBP



Fri, 02 Jan 2004 19:53:33 GMT  
 undocumented sequence

Quote:
> > Well, if (foobar == ""), as it does, then this is the same as:

> > awk '{print} '

> > what would you expect this to do?

> Nothing particular, I was just wondering how this may be correct.
> Because from the mawk manual page, I get:

> "An AWK program is a sequence of pattern {action} pairs and user
> function definitions.". And I did not know variables are expanded even
> out of any pattern or action block.

> Thnaks for your answer :)

Aha, I see.

Well if you try

awk 'BEGIN{foobar = 1} {print} foobar' <.kshrc

you should get each line of .kshrc printed twice because the first of each
is printed by {print} and the second by foobar (because it is non-zero and
non-null) using the default action of print.

Similarly

awk '1; 1' <.kshrc

will print each line twice using the default print action.

HTH

Peter
--


Opinions expressed are my own and not necessarily those of my employer



Fri, 02 Jan 2004 21:13:51 GMT  
 undocumented sequence
[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]


X  > Well, if (foobar == ""), as it does, then this is the same as:
X  >
X  > awk '{print} '
X  >
X  > what would you expect this to do?
X  
X  Nothing particular, I was just wondering how this may be correct.
X  Because from the mawk manual page, I get:
X  
X  "An AWK program is a sequence of pattern {action} pairs and user
X  function definitions.". And I did not know variables are expanded even
X  out of any pattern or action block.
X  
X  Thnaks for your answer :)

But it _IS_ in the 'pattern' block.  Lets reformat your line the way
awk sees it:

    awk '{print} foobar'

parses to

    awk '{print}  # action, but no pattern
         foobar   # pattern, but no action; implied print'

Awk does not require newlines between statements, so foobar is a
pattern.  An awk variable that has not been defined has a value of
zero, so the pattern 'foobar' is FALSE, so the implied action of
'print' is _NOT_ performed for 'foobar'.

                                        Bob Harris



Fri, 02 Jan 2004 21:43:42 GMT  
 undocumented sequence

% Hi all. I just discovered the following program seems to be valid for
% at least mawk and gawk, and I can't stand why:

[point of English -- you can't _understand_ why]

% awk '{print} foobar'
%
% I thought the only valid construction was something like
%
%       pattern {action}

Either the pattern or the action may be left out. If pattern is left out,
it is taken to be 1 (true). If action is left out, it is taken to be
{print}, which prints the current line. So, your program is equivalent
to

 1 { print }
 foobar { print }

which would print every line, then evaluate foobar and print every line
again if foobar is any value other than 0 or "". Since foobar is
always "" in your program, it should simply print each line of input.
You'll be pleast to know that this is equivalent to
 awk 1

% Is this a completely different and magic feature ? :)

You could say that. It's just to make it easier to type programs
in the common cases where you want to print all lines which match
a pattern, or you want to execute an action on every input line.

--

Patrick TJ McPhee
East York  Canada



Fri, 02 Jan 2004 22:02:59 GMT  
 undocumented sequence


Quote:


>% Hi all. I just discovered the following program seems to be valid for
>% at least mawk and gawk, and I can't stand why:

>[point of English -- you can't _understand_ why]

>% awk '{print} foobar'
>%
>% I thought the only valid construction was something like
>%
>%      pattern {action}

>Either the pattern or the action may be left out. If pattern is left out,
>it is taken to be 1 (true). If action is left out, it is taken to be
>{print}, which prints the current line. So, your program is equivalent
>to

> 1 { print }
> foobar { print }

>which would print every line, then evaluate foobar and print every line
>again if foobar is any value other than 0 or "". Since foobar is
>always "" in your program, it should simply print each line of input.
>You'll be pleast to know that this is equivalent to
> awk 1

>% Is this a completely different and magic feature ? :)

>You could say that. It's just to make it easier to type programs
>in the common cases where you want to print all lines which match
>a pattern, or you want to execute an action on every input line.

>--

>Patrick TJ McPhee
>East York  Canada


I have noticed that foobar is often used as a file name rather than a
variable name. This makes me think that

awk '{print} foobar'

maybe should have been

awk '{print}' foobar

which is roughly equivalent to cat foobar

--
Alan Linton



Sat, 03 Jan 2004 04:16:48 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Calling a sequence of commands with a sequence of Tkinter buttons

2. dll's undocumented export method.

3. dll's undocumented export method.

4. J3 undocumented...

5. Undocumented features...

6. Undocumented use of callbacks in VW

7. Undocumented feature?

8. errorcode() 300 from remove()? (undocumented)

9. C4 RW Bug or Undocumented Feature -- Help Please

10. Blob Tip (Undocumented Feature?)

11. Undocumented compilation error

12. Undocumented error

 

 
Powered by phpBB® Forum Software