Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

concatenating regular expressions
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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
04-18-05 01:55 AM


Re: concatenating regular expressions
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-18-05 01:55 AM


Re: concatenating regular expressions
In 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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
04-18-05 08:55 AM


Re: concatenating regular expressions
Bob 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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
04-18-05 08:55 AM


Re: concatenating regular expressions
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-18-05 08:55 AM


Re: concatenating regular expressions

Robert 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-18-05 08:55 PM


Re: concatenating regular expressions

Robert 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-21-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:17 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.