Jeff Bauersfeld
Jeff Bauersfeld

Reputation: 149

How can I make nawk evauluate two if conditions as one?

For the data set:

12345   78945
12345   45678

I need the output to be

12345   45678

Long story short, sometimes two values are representing the same object due to certain practices instituted above my pay grade. So with a list of known value-pairs that represent the same object, I need awk to filter these from the output. In the above situation 12345 and 78945 represent the same object, so it should be filtered out. The remaining rows are errors that should be brought to my attention.

My attempted code

cat data | nawk '(($1!="12345")&&($2!="78945"))'

produces an empty set as output. So either I'm committing a logical error in my mind or a syntactical one whereby nawk is evaluating each condition individually as if written like cat data | nawk '($1!="12345")&&($2!="78945")', thus filtering out both since both fail the first condition.

I'm sure it's just my unfamiliarity with how nawk resolves such things. Thanks in advance for any assistance. And for reasons, this has to be done in nawk.

Upvotes: 0

Views: 54

Answers (1)

Ed Morton
Ed Morton

Reputation: 204259

There is no line in your sample data for which $1!="12345" is true so there can be no condition where that && anything else can be true. Think about it. This has nothing to do with awk - it's simple boolean logic.

Try any of these instead, whichever you feel is clearer:

nawk '($1!="12345") || ($2!="78945")' data
nawk '!(($1=="12345") && ($2=="78945"))' data
nawk '($1=="12345") && ($2=="78945"){next} 1' data
nawk '($1" "$2) != "12345 78945"' data
nawk '!/^[ \t]*12345[ \t]+78945([ \t+]|$)/' data

Also google UUOC to understand why I got rid of cat data |.

Upvotes: 4

Related Questions