0

Here's what I want to run in theory

    awk 'BEGIN {FS=OFS=","}
               {if( $4 != "" || $4 != "Store Number")
               print $2}' $file >>out;

Instead I get all records from $file (i.e. no conditions applied)

However, I have checked that the following works:

    awk 'BEGIN {FS=OFS=","}
               {if( $4 != "")
               print $2}' $file >>out;

Tried most combinations of phrasing including !(cond1 || cond2), !~, if((cond1) || (cond2)), but none work. I am running awk in Windows 10 bash.

wip
  • 17
  • 5
  • 3
    `$4 != "" || $4 != "Store Number"` is always true. A string is always either not empty or not some specific string. Did you mean `&&`? – Benjamin W. Dec 19 '17 at 18:31
  • 1
    `awk 'BEGIN {FS=OFS=","} $4 != "" && $4 != "Store Number"' "$file"` should work – anubhava Dec 19 '17 at 18:32
  • 1
    I want a fruit where that fruit is not a banana OR not a strawberry. So... any fruit at all (a strawberry is not a banana, therefore it passes the filter, and a banana is not a strawberry so it too passes the filter). You want an AND `&&`instead. – JNevill Dec 19 '17 at 18:33

2 Answers2

1
$4 != "" || $4 != "Store Number"

The only string that can fail that condition is one that is both "" and "Store Number" at the same time!

Since that is clearly not possible, it won't filter anything, as you have seen.

You probably meant to use &&.


As an aside, you can simplify your code a little if you use the condition part of awk:

BEGIN {FS=OFS=","}
$4 != "" && $4 != "Store Number" {print $2}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

not a or not b includes everything (unless a==b).

Answer: !=a && !=b

Thanks for the quick answers;; Time to brush up on my sets/logic.

wip
  • 17
  • 5