如何将文件的字段与当前时间戳进行比较并打印更大或更小的数据? [英] How to compare a field of a file with current timestamp and print the greater and lesser data?

查看:96
本文介绍了如何将文件的字段与当前时间戳进行比较并打印更大或更小的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较当前时间戳和文件字段并打印匹配和不匹配的数据.我在文件中有2列(见下文)

How do I compare current timestamp and a field of a file and print the matched and unmatched data. I have 2 columns in a file (see below)

oac.bat 09:09
klm.txt 9:00

我想将时间戳(第二列)与当前时间进行比较,例如假设(10:00),并按如下所示打印输出.

I want to compare the timestamp(2nd column) with current time say suppose(10:00) and print the output as follows.

在10:00

greater.txt

xyz.txt 10:32
mnp.csv 23:54

Lesser.txt

oac.bat 09:09
klm.txt 9:00

有人可以帮我吗?

我使用了awk $0 > "10:00",它只给我第二列的详细信息,但是我想同时获得列的详细信息,所以我直接从系统中获取带有诸如

I used awk $0 > "10:00", which gives me only 2nd column details but I want both the column details and I am taking timestamp from system directly from system with a variable like

d=`date +%H:%M`

推荐答案

使用GNU awk,您可以使用它的内置时间函数:

With GNU awk you can just use it's builtin time functions:

awk 'BEGIN{now = strftime("%H:%M")} {
    split($NF,t,/:/)
    cur=sprintf("%02d:%02d",t[1],t[2])
    print > ((cur > now ? "greater" : "lesser") ".txt")
}' file

对于其他awk,只需预先使用-vdate设置now,例如:

With other awks just set now using -v and date up front, e.g.:

awk -v now="$(date +"%H:%M")" '{
    split($NF,t,/:/)
    cur = sprintf("%02d:%02d",t[1],t[2])
    print > ((cur > now ? "greater" : "lesser") ".txt")
}' file

以上内容未经测试,因为您没有提供我们可以测试的输入/输出.

The above is untested since you didn't provide input/output we could test against.

这篇关于如何将文件的字段与当前时间戳进行比较并打印更大或更小的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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