For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2007 > boolean expr









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 boolean expr
Sashi

2007-11-30, 6:58 pm

gawk -F'\t' '($1 != "LATIN AMERICA")'
The above prints all lines for whom the first field isn't LATIN
AMERICA.
Fine.

gawk -F'\t' ' ($13 !~ /Sublet/)'
You guessed it! Prints all lines for whom the 13th field isn't Sublet.

gawk -F'\t' '($1 != "LATIN AMERICA" || $13 !~ /Sublet/)'
The above prints ALL records.

I want to print only those lines which are or'd according to the above
two conditions.

Can someone please point my error?

Thanks,
Sashi
Kenny McCormack

2007-11-30, 6:58 pm

In article <2408b4dd-de8f-4032-8ee9-e44318fec40b@j44g2000hsj.googlegroups.com>,
Sashi <smalladi@gmail.com> wrote:
>gawk -F'\t' '($1 != "LATIN AMERICA")'
>The above prints all lines for whom the first field isn't LATIN
>AMERICA.
>Fine.
>
>gawk -F'\t' ' ($13 !~ /Sublet/)'
>You guessed it! Prints all lines for whom the 13th field isn't Sublet.
>
>gawk -F'\t' '($1 != "LATIN AMERICA" || $13 !~ /Sublet/)'
>The above prints ALL records.
>
>I want to print only those lines which are or'd according to the above
>two conditions.
>
>Can someone please point my error?
>
>Thanks,
>Sashi


Congratulations! You have analyzed and coded the problem perfectly.
You have found the perfect solution to printing out all the lines that
are not "LATIN AMERICA" or are not "Sublet". Excellent!

Sashi

2007-11-30, 6:58 pm

On Nov 30, 11:04 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:


> Congratulations! You have analyzed and coded the problem perfectly.
> You have found the perfect solution to printing out all the lines that
> are not "LATIN AMERICA" or are not "Sublet". Excellent!


Thank you, Kenny. Your sarcasm is greatly appreciated.
Ed Morton

2007-11-30, 6:58 pm

On 11/30/2007 9:59 AM, Sashi wrote:
> gawk -F'\t' '($1 != "LATIN AMERICA")'
> The above prints all lines for whom the first field isn't LATIN
> AMERICA.
> Fine.
>
> gawk -F'\t' ' ($13 !~ /Sublet/)'
> You guessed it! Prints all lines for whom the 13th field isn't Sublet.
>
> gawk -F'\t' '($1 != "LATIN AMERICA" || $13 !~ /Sublet/)'
> The above prints ALL records.


Applying boolean algebra, the above is probably more clearly written as:

gawk -F'\t' '!($1 == "LATIN AMERICA" && $13 ~ /Sublet/)'

> I want to print only those lines which are or'd according to the above
> two conditions.
>
> Can someone please point my error?


It looks fine to me, but then I don't know what you really wanted to do. I guess
in your input every time $1 == "LATIN AMERICA", $13 !~ /Sublet/ and vice-versa.

Did you mean that you wanted output for only those lines where neither $1 ==
"LATIN AMERICA" nor $13 ~ /Sublet/? If so, that'd be

gawk -F'\t' '!($1 == "LATIN AMERICA" || $13 ~ /Sublet/)'

or equivalently:

gawk -F'\t' '$1 != "LATIN AMERICA" && $13 !~ /Sublet/'

or even:

gawk -F'\t' '$1 == "LATIN AMERICA" || $13 ~ /Sublet/{next} {print}'

If you'd like to post some sample input and expected output, maybe we could help
more....

Ed.

Sponsored Links







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

Copyright 2008 codecomments.com