For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2006 > logical operator problem in AWK









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 logical operator problem in AWK
subPlanck@excite.com

2006-02-27, 6:57 pm

I'd like to select only certain lines for further processing, but the
first statement doesn't work (i.e., its second half). I'm trying to
select any combination of fields 10 and 11 as specified below.

(($10=="C"||$10=="D")&&($11==132||$11==133)) ||
(($10=="C"||$10=="O")&&($11==129||$11==193)) {...

The following statement works fine:

( $10 == "C" || $10 == "D" ) && ( $11 == 132 || $11 == 133 ) {...

Which awk/C rule am i breaking?

z.entropic

Bob Harris

2006-02-27, 9:55 pm

In article
<1141066333.410506.322720@i39g2000cwa.googlegroups.com>,
subPlanck@excite.com wrote:

> I'd like to select only certain lines for further processing, but the
> first statement doesn't work (i.e., its second half). I'm trying to
> select any combination of fields 10 and 11 as specified below.
>
> (($10=="C"||$10=="D")&&($11==132||$11==133)) ||
> (($10=="C"||$10=="O")&&($11==129||$11==193)) {...
>
> The following statement works fine:
>
> ( $10 == "C" || $10 == "D" ) && ( $11 == 132 || $11 == 133 ) {...
>
> Which awk/C rule am i breaking?
>
> z.entropic


Is the first statement 2 lines?

If so, then you need to use a backslash as a continuation character

(($10=="C"||$10=="D")&&($11==132||$11==133)) || \
(($10=="C"||$10=="O")&&($11==129||$11==193)) {...

Bob Harris
Ed Morton

2006-02-28, 3:55 am

Bob Harris wrote:
> In article
> <1141066333.410506.322720@i39g2000cwa.googlegroups.com>,
> subPlanck@excite.com wrote:
>
>
>
>
> Is the first statement 2 lines?
>
> If so, then you need to use a backslash as a continuation character
>
> (($10=="C"||$10=="D")&&($11==132||$11==133)) || \
> (($10=="C"||$10=="O")&&($11==129||$11==193)) {...
>
> Bob Harris


and if you want string rather than numeric comparison of all fields, you
could abbreviate to:

(($10~/^[CD]$/)&&($11~/^13[23]$/)) || \
(($10~/^[CO]$/)&&($11~/^1(29|93)$)) {...

Regards,

Ed.
William James

2006-02-28, 3:55 am

Bob Harris wrote:
> In article
> <1141066333.410506.322720@i39g2000cwa.googlegroups.com>,
> subPlanck@excite.com wrote:
>
>
> Is the first statement 2 lines?
>
> If so, then you need to use a backslash as a continuation character
>
> (($10=="C"||$10=="D")&&($11==132||$11==133)) || \
> (($10=="C"||$10=="O")&&($11==129||$11==193)) {...
>


Absolutely wrong. The "||" at the end of the line tells awk
that the expression is incomplete. Similarly, this is fine:

{ print 1, 2,
3
}

Didn't "The AWK Programming Language" make that clear
to you?

subPlanck@excite.com

2006-03-01, 6:56 pm


William James wrote:
> Bob Harris wrote:
>
> Absolutely wrong. The "||" at the end of the line tells awk
> that the expression is incomplete. Similarly, this is fine:
>
> { print 1, 2,
> 3
> }
>
> Didn't "The AWK Programming Language" make that clear
> to you?


Gentlemen,

No need to be so unpleasant, Ed has been a valuable contributor to this
group for years.

The long line in my example is a single line, with no continuation
character. Google Groups just split it without my control...

Thus, what's wrong with my expression? Is the AWK program line length
limited to 72/80 etc. characters? My screen is 125 characters long,
and I'm taking advantage of that...

z.entropic

subPlanck@excite.com

2006-03-01, 6:56 pm

Bob Harris asks:

[color=darkred]
> Is the first statement 2 lines?


No, it's a single line (I should have used a backslash to make it
clear; sorry).

z.entropic

gerryt@

2006-03-01, 6:56 pm


subPlanck@excite.com wrote:

> Gentlemen,


?? huh? where?? : /

> The long line in my example is a single line, with no continuation
> character. Google Groups just split it without my control...


Yes. They do that. Quite annoying

> Thus, what's wrong with my expression? Is the AWK program line length
> limited to 72/80 etc. characters? My screen is 125 characters long,
> and I'm taking advantage of that...


I dont think you broke any rules but even a crud C programmer like me
knows about
precedence and ambiguity so I tend to use LOTS of brackets. If you
enclose the
entire test in an another pair of ()'s you may find your awk script
works. Hard to say
as you didnt give enough code for anyone to be certain about what you
are really doing.
Same goes for sample input, so we have to guess at what might be the
desired output.
Anyway even oawk on Solaris worked on my guess test.

Kees Nuyt

2006-03-01, 6:56 pm

On 1 Mar 2006 09:49:55 -0800, subPlanck@excite.com wrote:

>Bob Harris asks:
>
>
>
>No, it's a single line (I should have used a backslash to make it
>clear; sorry).
>
>z.entropic


Though not strictly necessary, I would use a few more
parenthesis to make the expression even less ambiguous:

(((($10=="C") || ($10=="D")) && (($11==132) || ($11==133))) ||
((($10=="C") || ($10=="O")) && (($11==129) || ($11==193)))){...
--
( Kees
)
c[_] I used to have an open mind but my brains kept falling out. (#76)
Patrick TJ McPhee

2006-03-01, 9:55 pm

In article <1141066333.410506.322720@i39g2000cwa.googlegroups.com>,
<subPlanck@excite.com> wrote:

[...]

% (($10=="C"||$10=="D")&&($11==132||$11==133)) ||
% (($10=="C"||$10=="O")&&($11==129||$11==193)) {...
%
% The following statement works fine:
%
% ( $10 == "C" || $10 == "D" ) && ( $11 == 132 || $11 == 133 ) {...

This pattern clearly matches a subset of the records matched by
the first pattern, so I'd say that something you haven't posted
is causing your problem.

--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Marek Simon

2006-03-02, 9:55 pm

subPlanck@excite.com napsal(a):
> I'd like to select only certain lines for further processing, but the
> first statement doesn't work (i.e., its second half). I'm trying to
> select any combination of fields 10 and 11 as specified below.
>
> (($10=="C"||$10=="D")&&($11==132||$11==133)) ||
> (($10=="C"||$10=="O")&&($11==129||$11==193)) {...
>
> The following statement works fine:
>
> ( $10 == "C" || $10 == "D" ) && ( $11 == 132 || $11 == 133 ) {...
>
> Which awk/C rule am i breaking?
>
> z.entropic
>


I am not sure if you wanted to use letter "O" in the second line.
And there is no need of backslash if the line ends with a logical operator.
Marek
Sponsored Links







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

Copyright 2008 codecomments.com