[英]Count day difference between two dates ( in YYYY-DD-MM HH:MM:SS format) from two different columns in unix [closed]
I have tsv file with two column contains dates in YYYY-DD-MM HH:MM:SS format.我有两列的 tsv 文件包含 YYYY-DD-MM HH:MM:SS 格式的日期。
I want to count the day difference between these dates (as Truncated Integer)我想计算这些日期之间的天差(作为截断整数)
for example:例如:
input:输入:
column1 column2
2013-04-26 12:38:33 2013-04-27 12:38:33.757
2019-06-07 17:46:02.543 2019-06-27 22:46:02.543
2013-02-06 12:38:33 2013-02-18 22:50:50
Output: Output:
1 1个
20 20
12 12
My dates in column 1 and 2 I tried我在第 1 列和第 2 列中尝试过的日期
gawk -F "\t" '
{print$0 "\t"
split($1,a,"-" || ":" );
split($2,b,"-" || ":" );
t1 = mktime(sprintf("%d %d %d 0 0 0 0",a[1],a[2],a[3]));
t2 = mktime(sprintf("%d %d %d 0 0 0 0",b[1],b[2],b[3]));
print (t2-t1)/86400
}'
But I got strange number which not reflecting the accurate day difference.但是我得到了一个奇怪的数字,它没有反映准确的日差。
I'm guessing you mean a.csv file (comma-separated value)?我猜你是说 a.csv 文件(逗号分隔值)? I would use R to do this.
我会用R来做到这一点。 If you install R (it is a free as in beer download), you should get the Rscript command with it, then you can can use the
as.Date()
method and do this from the command line or a script如果你安装了 R(它是免费的 beer 下载),你应该得到 Rscript 命令,然后你可以使用
as.Date()
方法并从命令行或脚本执行此操作
Rscript -e 'as.Date("2013-04-27 12:38:33.757") - as.Date("2013-04-26 12:38:33")'
Time difference of 1 days
Dealing with dates and time differences is devilishly complicated.处理日期和时间差异非常复杂。
When you need to do it multiple times, it's cleaner/easier to define your own function for converting your date format to epoch time.当您需要多次执行此操作时,定义自己的函数以将日期格式转换为纪元时间会更清晰/更容易。
printf '%s\t%s\n' \
'2013-04-26 12:38:33' '2013-04-27 12:38:33.757' \
'2019-06-07 17:46:02.543' '2019-06-27 22:46:02.543' \
'2013-02-06 12:38:33' '2013-02-18 22:50:50' |
awk -F '\t' '
function epoch(str, i) {
gsub(/[-:]/," ",str)
return mktime(str) ((i = index(str,".")) ? substr(str,i) : "")
}
{ print int((epoch($2) - epoch($1)) / 86400) }
1
20
12
notes:笔记:
mktime
doesn't support floating point precision so I re-append the sub-zero part. mktime
不支持浮点精度,所以我重新附加了低于零的部分。
print int(float)
truncates the sub-zero part of float
; print int(float)
截断float
的零以下部分; if your prefer a rounded output then use printf "%d%s", float, ORS
如果您更喜欢圆形输出,请使用
printf "%d%s", float, ORS
Merry Christmas!圣诞节快乐!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.