awk help - matching a field with certail values

Hello there,

I have a file with few fields separated by ":". I wrote a below awk to manipulate this file:

awk 'BEGIN { FS=OFS=":" }\
NR != 1 && $2 !~ /^98/ && $8 !~ /^6[12][01][0]/{print $0}' $in_file > $out_file

What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 - it should not be processed. But above pattern will match to 6210 as well and will reject it. What modification should I make in order for pattern not to select 6210 and all 6210s appear in the output?

regards,
juzz4fun

awk 'BEGIN { FS=OFS=":" }
NR != 1 && $2 !~ /^98/ && $8 !~ /^6(10|11|20)0/' $in_file > $out_file

will copy lines from $infile to output if and only if:

  1. it is not the 1st line of the file,
  2. the second field in the file does not start with "98", and
  3. the eighth field in the file does not start with 6100, 6110, or 6200.

Note that item #3 in the list says "start with" (which is what your code was doing), but your English description said "contains". If you want "contains" instead of "starts with", change /^6(10|11|20)0/ to /6(10|11|20)0/ . If you want "is" instead of "starts with" or "contains", change $8 !~ /^6(10|11|20)0/ to $8 != /6(10|11|20)0/ .

Thanks Don Cragun. That helps.
I was looking for line that "starts with" only.. sorry for that... :slight_smile: