awk match multiple pattern in column -
what proper awk syntax match multiple patterns in 1 column? having columnar file this:
c11 c21 c31 c12 c22 c32 c13 c23 c33 how exclude lines match c21 , c22 in second column.
with grep, 1 can (but doesn't specify match in second column only):
> egrep -w -v "c21|c22" bar.txt c13 c23 c33 i tried playing awk no avail:
> awk '$2 != /c21|c22/' bar.txt c11 c21 c31 c12 c22 c32 c13 c23 c33 > awk '$2 != "c21" || $2 != "c22"' bar.txt c11 c21 c31 c12 c22 c32 c13 c23 c33 so, proper awk syntax right?
$2 != /c21|c22/ is shorthand for
$2 != ($0 ~ /c21|c22/) which comparing $2 result of comparing $0 c21 or c22 , result either 1 or 0 it's testing $2 having value other 1.
$2 != "c21" || $2 != "c22" is testing $2 not equal c21 or $2 not equal c22 condition true. think - if $2 c21 first condition ($2 != "c21") false second condition ($2 != "c22") true , on or true value of $2
what you're trying write is:
awk '$2 !~ /c21|c22/' or more robustly:
awk '$2 !~ /^(c21|c22)$/' and more briefly (plus robustly) way write condition is:
awk '$2 !~ /^c2[12]$/' and if wanted string rather regexp comparison you'd either of these if it's throwaway script (i favor first fewer negation signs imho makes clearer):
awk '!($2 == "c21" || $2 == "c22")' awk '$2 != "c21" && $2 != "c22"' and otherwise:
awk 'begin{split("c21 c22",t); (i in t) vals[t[i]]} !($2 in vals)' that last best since specify $2 once , can add other values string being split if need test more means can't break comparison ogic later in script.
Comments
Post a Comment