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开发者_如何学C 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)
精彩评论