2

I am trying to use awk command to find line(s) which the third columns is not digit/date? Suppose there is a file comma "," field separated, has three columns and as code "," measure "," dd/mm/yyyy,

97xx574,26.7,12/30/1997,
97xy575,18,12/30/1997,
code,meas,EXAMDATE,
B529ui,28.2,12/30/1997,
B530sx,26.4,12/30/1997,
J487sxv,21.5,12/30/1997,

I tried:

awk -F "," '/$3[^0-9].*/ {print $0}' <filename> 

... but apparently it is not correct!

5

How about this. Where 3rd field does not consist of 0-9 or /, print the line (which is the default action : no need for a print $0.

$3 = third field
!~ = where does not (!) match regular expression
/  = mark start of regular expression
^  = match start of field
[0-9/]+ = match any of the 0123456789/ characters at least once
$  = match end of field
/  = mark end of regular expression

So code, with output:

awk -F, '$3!~/^[0-9/]+$/' filename
code,meas,EXAMDATE,

Introducing a bit more checking, so has to consist of nn/nn/nnnn, try this.

awk -F, '$3!~/^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/' filename
code,meas,EXAMDATE,

Could even use grep if you wanted.

grep -vE '^.*,.*,[0-9/]+,$' x1
code,meas,EXAMDATE,
  • Note that this will fail if pre-October months are written with only one digit (or if the first nine days of a month are written with one digit). Still, it's good. – Wildcard Nov 22 '16 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.