awk用于查找两列中的日期差异

时间:2018-01-15 10:02:51

标签: awk

这样的日期
20 20180110 20180113
100 20180110 20180115
9 20180110 20180114

期望的输出

20 20180110 20180113 3
100 20180110 20180115 5
9 20180110 20180114 4

请帮助我使用awk命令。

2 个答案:

答案 0 :(得分:0)

尝试按照awk代码告诉我这是否有帮助。它仅在GNU TZ=PST awk ' { split($2,date,""); split($3,date1,""); print $0,(mktime(date1[1] date1[2] date1[3] date1[4]" " date1[5] date1[6]" " date1[7] date1[8] " " "00 00 00")\ -mktime(date[1] date[2] date[3] date[4]" " date[5] date[6]" " date[7] date[8] " " "00 00 00"))\ /(24*60*60); }' Input_file 上创建和测试。

REFLIST_OP=/tmp/abc.lst 
cat $REFLIST_OP      
34 /data/abc/GOon/expdp_TEST_P119_*_18112017.dmp       12-JAN-18 04.27.00 AM

答案 1 :(得分:0)

使用 GNU awk mktime()substr()功能

输入:

$ cat file
20 20180110 20180113
100 20180110 20180115
9 20180110 20180114

输出:

awk 'function df(v){ return mktime(substr(v,1,4)" "substr(v,5,2)" "substr(v,7,2)" "0" "0" "0)  }{print $0,(df($3)-df($2))/86400}' file
20 20180110 20180113 3
100 20180110 20180115 5
9 20180110 20180114 4

更好的可读性:

awk '
    function df(v)
    {
      return mktime(substr(v,1,4)" "substr(v,5,2)" "substr(v,7,2)" "0" "0" "0)  
    }
    {
      print $0,(df($3)-df($2))/86400
    }
    ' file