awk从两列中查找日期差 [英] awk to find date difference from two column

查看:77
本文介绍了awk从两列中查找日期差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件具有以下格式的值-

I have file with values in below format-

datecolumn1     datecolumn2
20140202 20180113
20140202 20180113
20140202 20180113
20131202 20180113
20140331 20180113

我想减去$ 3- $ 2来找到总天数.输出将如下-

I want to subtract $3-$2 to find total number of days. output will be as below-

20180113 20180115 3.

请与awk命令一起使用.

please help with awk command.

推荐答案

跟随 awk 可能会对您有所帮助,以获取日期之间的天数差(您发布在帖子中的格式).同样,该代码是在GNU awk 中创建和测试的.

Following awk may help you in same, to get the difference of days between dates(format which you have posted in your post). Also this code is created and tested in GNU awk.

TZ=PST awk '
FNR>1{
  split($1,date,"");
  split($2,date1,"");
  print (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

这篇关于awk从两列中查找日期差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆