Home > Archive > AWK > April 2005 > concatenating regular expressions
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 |
concatenating regular expressions
|
|
| Robert Katz 2005-04-17, 8:55 pm |
| 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
| |
| Chris F.A. Johnson 2005-04-17, 8:55 pm |
| 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
| |
| Bob Harris 2005-04-18, 3:55 am |
| 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
| |
| Robert Katz 2005-04-18, 3:55 am |
| 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
| |
| Chris F.A. Johnson 2005-04-18, 3:55 am |
| 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?
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
| |
| Ed Morton 2005-04-18, 3:55 pm |
|
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.
| |
| Ed Morton 2005-04-20, 8:55 pm |
|
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.
|
|
|
|
|