if
语句。if
语句。我有这个输入
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
我使用了这段代码,但它仅适用于多年:
awk '{if ($1+1==$2) print }'
答案 0 :(得分:2)
在mktime
中查看awk
funktin
有了它,您可以将日期转换为秒,以便于比较
这会打印$1
和$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
她打印了一天的1
,如果是一个月打印2
计算二月可能有28
或29
天
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