views:

97

answers:

4

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

A: 

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
Trevor Tippins
A: 

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)

mouviciel
@Bq: btw, these are called De Morgan's Laws. http://en.wikipedia.org/wiki/De_Morgan's_laws
Trevor Tippins
Thanks for the link, that was what I was looking for :)
Balroq
+1  A: 

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

kriss
+1  A: 

why don't you do it this way

awk '/COREPORT/{next}1' file
ghostdog74
+1. That had me wondering too. Why not look for exactly what you're trying to look for instead of half at a time?
JUST MY correct OPINION