0
  1. using if statement if field one + 1 day equal field two.
  2. using if statement if field one + 1 month equal field two.

I have this input

09-11-2013 09-12-2013 10-02-2013 10-02-2013 26-10-2013 27-10-2013 12-01-2013 12-02-2013 22-02-2013 23-02-2013 

I used this code but it works with years only:

awk '{if ($1+1==$2) print }' 

1 Answer 1

2

Have a look at the mktime funktin in awk
With it you can convert date to seconds so it be easy to compare.

This prints how many days there are between $1 and $2

awk '{split($1, sd, "-");split($2, ed, "-");print $0,(mktime(ed[3] s ed[2] s ed[1] s 0 s 0 s 0)-mktime(sd[3] s sd[2] s sd[1] s 0 s 0 s 0))/86400}' s=' ' file 09-11-2013 09-12-2013 30 10-02-2013 10-02-2013 0 26-10-2013 27-10-2013 1 12-01-2013 12-02-2013 31 22-02-2013 23-02-2013 1 

Her it prints 1 of its one day, and 2 if its one month.
It take in count that February may have 28 or 29 days

awk ' BEGIN { arr="31,28,31,30,31,30,31,31,30,31,30,31" split(arr, month, ",") x=0} { split($1, sd, "-") split($2, ed, "-") t=(mktime(ed[3] s ed[2] s ed[1] s 0 s 0 s 0)-mktime(sd[3] s sd[2] s sd[1] s 0 s 0 s 0))/86400 month[2]=sd[3]%4==0?29:28 } t==month[sd[2]+0] {x=2} t==1 {x=1} {print $0,x x=0} ' s=' ' file 09-11-2013 09-12-2013 2 10-02-2013 10-02-2013 0 26-10-2013 27-10-2013 1 12-01-2013 12-02-2013 2 22-02-2013 23-02-2013 1 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.