Code Comments
Programming Forum and web based access to our favorite programming groups.Can someone explain this output?
$ print 'a b c' | awk '{ print gsub(/x/, "") }'
0
$ print 'a b c' | awk '{ print gsub(/x/, "") gsub(/y/, "") }'
00
$ print "a b c" | awk '0'
$ print "a b c" | awk '00'
$ print "a b c" | awk 'gsub(/x/, "")'
$ print "a b c" | awk 'gsub(/y/, "")'
$ print "a b c" | awk 'gsub(/x/, "") gsub(/y/, "")'
a b c
--
Regards,
---Robert
Post Follow-up to this messageOn Sun, 17 Apr 2005 at 22:06 GMT, Robert Katz wrote:
> Can someone explain this output?
>
> $ print 'a b c' | awk '{ print gsub(/x/, "") }'
> 0
>
> $ print 'a b c' | awk '{ print gsub(/x/, "") gsub(/y/, "") }'
> 00
>
> $ print "a b c" | awk '0'
> $ print "a b c" | awk '00'
>
> $ print "a b c" | awk 'gsub(/x/, "")'
> $ print "a b c" | awk 'gsub(/y/, "")'
> $ print "a b c" | awk 'gsub(/x/, "") gsub(/y/, "")'
> a b c
See my response to your other post ("0 0" is a string).
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Post Follow-up to this messageIn article <u5B8e.4031$z9.355@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:
> Can someone explain this output?
>
> $ print 'a b c' | awk '{ print gsub(/x/, "") }'
> 0
Default Pattern, print return value from gsub, aka 0, no match found
> $ print 'a b c' | awk '{ print gsub(/x/, "") gsub(/y/, "") }'
> 00
Default Pattern, concatenate results from 2 gsub calls (00 because there
was no result found for either substitution), print concatenated result
> $ print "a b c" | awk '0'
> $ print "a b c" | awk '00'
Default Action, do nothing because pattern is 0, aka FALSE
> $ print "a b c" | awk 'gsub(/x/, "")'
> $ print "a b c" | awk 'gsub(/y/, "")'
Default Action, do nothing because gsub returned 0 (no match) and that
is the Pattern
> $ print "a b c" | awk 'gsub(/x/, "") gsub(/y/, "")'
> a b c
This one is tricky. Both gsub's return 0, because they do not find
anything, _HOWEVER_, you then concatenate the result with is a string
operation, you do not have the number 00, you have the string "00" and
it is not a null string, so you non-null strings are treated a TRUE,
thus, the Default Action is taken, aka print $0
The basic rule of Grep is:
Pattern { Action }
Action performed if Pattern is true
Default Action: print $0
Example:
/a/
$1 == "a"
$0 == "a b c"
NF != 0
Default Pattern: true
Example:
{ gsub(/a/,""); print }
{ gsub(/x/,""); print }
{ x = 2 + 2; print x }
I suggest you read the following web page:
<http://h30097.www3.hp.com/docs/base...B_HTML/ARH9WBTE
/WKXXXXXX.HTM>
I think it will help a lot in getting a starting understanding of Awk.
After that, if you really want to learn more "The Awk Programming
Language" book, by Aho,,Kernighan Weinberger is very good, but a bit
expensive.
Bob Harris
Post Follow-up to this messageBob Harris wrote: > In article <u5B8e.4031$z9.355@news.cpqcorp.net>, > Robert Katz <katz@hp.com> wrote: > > > > > Default Action, do nothing because pattern is 0, aka FALSE > > > > > Default Action, do nothing because gsub returned 0 (no match) and that > is the Pattern > > > Here is really the essence of my question. Is this what happens? Since in the line $ print "a b c" | awk 'gsub(/x/, "")' no substitutions are made, the line becomes equivalent to $ print "a b c" | awk '0' which will not print anything, but two consecutive returns get concatenated to a string, so that $ print "a b c" | awk 'gsub(/x/, "") gsub(/y/, "")' becomes $ print "a b c" | awk '"00"' a b c and not $ print "a b c" | awk '00' > > This one is tricky. Both gsub's return 0, because they do not find > anything, _HOWEVER_, you then concatenate the result with is a string > operation, you do not have the number 00, you have the string "00" and > it is not a null string, so you non-null strings are treated a TRUE, > thus, the Default Action is taken, aka print $0 Does this imply that 'expr expr ... ' will always print regardless of the return values of any of the expressions? -- Regards, ---Robert
Post Follow-up to this messageOn Mon, 18 Apr 2005 at 02:21 GMT, Robert Katz wrote:
> Bob Harris wrote:
>
> Does this imply that
> 'expr expr ... '
>
> will always print regardless of the return values of any of the expressions?[/colo
r]
If the string concatenation is not empty, yes; but not for:
awk 'substr("a",1,0) substr("x",1,0)'
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Post Follow-up to this messageRobert Katz wrote: <snip> > Does this imply that > 'expr expr ... ' > > will always print regardless of the return values of any of the > expressions? > No. Not if the result of expr expr is itself the null string, e.g. if each expr results in a null string it's the same as: PS1> print "a b c" | gawk '""' PS1> print "a b c" | gawk '""""' which doesn't print. See my answer in your previous thread for details. Ed.
Post Follow-up to this messageRobert Katz wrote: <snip> > Does this imply that > 'expr expr ... ' > > will always print regardless of the return values of any of the > expressions? > No. Not if the result of expr expr is itself the null string, e.g. if each expr results in a null string it's the same as: PS1> print "a b c" | gawk '""' PS1> print "a b c" | gawk '""""' which doesn't print. See my answer in your previous thread for details. Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.