Ashish
Ashish

Reputation: 1952

AWK script to combine multiple-field validations

I have a file with 7 columns with caret (^) separator example

ABCD123456^12345678^192.654^02Aug15^12:10:36

I wrote an awk command which will check duplicates

awk '!seen[$0]++' filename

Length of first field should be 10 & second should be 8

awk -f '^' '{ if ((length($1) == 10) && (length($1) == 10)) print }' 

Question is

  1. How can I do time validation ([0-9]:[0-9]:[0-9])? what awk query

    awk -F '^' '$5 ~ [0-9]:[0-9]:[0-9]' print

    But this is not working

  2. Date Validation. How can I validate 2 digit day, 3 Char Month & 2 digit year

  3. Decimal Number validation in 3rd field

  4. How can I combine this all in single awk script

Upvotes: 1

Views: 301

Answers (1)

anubhava
anubhava

Reputation: 786101

You can combine them into one awk command like this:

awk -F '^' 'length($1) == 10 && length($2) == 8 &&
     $3 ~ /^[0-9]+\.[0-9]+$/ && $5 ~ /^[0-9]{2}:[0-9]{2}:[0-9]{2}$/ && !seen[$0]++' file

Upvotes: 2

Related Questions