English 中文(简体)
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

最佳回答

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.

  • !PORT means the line does not contain PORT
  • !CORE means the line does not contain CORE

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

问题回答

why don t you do it this way

awk  /COREPORT/{next}1  file

Truth Table coming up...

CORE   PORT   !CORE   !PORT   AND(!CORE,!PORT)  OR(!CORE,!PORT)
 T       T      F       F            F                F
 T       F      F       T            F                T
 F       T      T       F            F                T
 F       F      T       T            T                T

A good way for visualising logic is Karnaugh map.

Or, if you want to handle math expressions, just remember that:

  • not (a and b) is the same as (not a) or (not b)
  • not (a or b) is the same as (not a) and (not b)

Actually, what you want is not: (not CORE) and (not PORT) but: not (CORE and PORT) which is the same as: (not CORE) or (not PORT)





相关问题
How do I convert a MySQL function result to tinyint(1)

Here s the problem. In MySQL s Connector/NET a TINYINT(1) field properly translates back and forth into a .NET bool value. If I select from a table with a TINYINT(1) column, everything is golden. ...

How do I use a Boolean in Python?

Does Python actually contain a Boolean value? I know that you can do: checker = 1 if checker: #dostuff But I m quite pedantic and enjoy seeing booleans in Java. For instance: Boolean checker; ...

Pass a Boolean Ada type in Interfaces.C

I would like to now how to pass a standard Boolean Type in Ada through the Interfaces.C package in order to call a DLL function. The Interfaces.C package does not contain the Ada Boolean type since ...

C# bool expression evaluation order [duplicate]

Possible Duplicate: == Operator and operands Possible Duplicates: Is there any difference between if(a==5) or if(5==a) in C#? == Operator and operands Ok, this may be stupid question, ...

Boolean Expression Evaluation in Java

I m looking for a relatively simpler (when compared with writing a parser) way to evaluate boolean expressions in Java, and I do not want to use the JEP library. I have a String expression like: (x &...

Should I use 0/1 or True/False boolean? [duplicate]

0/1 can be flexible and can add options such as "2, 3, 4" in the future. Does TINYINT in MySQL take up more space than boolean? Personally, I use 0 and 1 for everything. You can answer this ...

热门标签