awkbooleanboolean-logicboolean-expression

I cant really wrap my head around BOOLEAN logic when I use NOT together with AND and OR


Im trying to understand how boolean logic works when I use NOT. To give an example using awk

I have a text file containing

CORE
PORT
CORE 
PORT
COREPORT
CORE
COREPORT

And I would like to remove all COREPORT lines. The way I thought I would do it was with (NOT CORE) AND (NOT PORT) eg

awk '/!CORE/&&/!PORT/{print}'

But when I try it out Im actually supposed to use OR instead of AND

awk '/!CORE/||/!PORT/{print}'

I would be really glad if some one could explain where my thinking is wrong and super glad if it could be visualized with a venn diagram or something like the boolean machine at kathyschrock


Solution

  • I will try to give a gut feeling or your boolean expressions, for maths other posters did it very well.

    Your boolean expression must be true for the lines you want to keep.

    Hence your boolean expression means keep the lines that as the same time does not contains PORT and does not contains CORE. Obviously there is no such lines in your file...

    You must use or because what you truly want to express is keep the lines that does not contain both PORT and CORE, but as you can see there is only one negation in the above statement. You are trying to say something like: does line contain PORT, does it also contains CORE then I do not want it. And that is !(/CORE/ && /PORT/), and using boolean math you can also write that /!CORE/||/!PORT/ as you have seen by yourself.

    Generally speaking negative assertions are difficult to understand. I'm not the only one to say that. For example, Damian Conway in Perl Best Practice pointed it out and recommanded using positive statements whenever possible (and use unless Perl operator instead of if when you want to negate a condition).