简体   繁体   English

如何使用awk shell脚本(如下所示)对时间A(格式为“ hh:mm:ss”)和时间B之间的行进行排序?

[英]How to sort rows between time A (in format “hh:mm:ss”)and time B using awk shell script (like below?)?

I want to sort rows between time A and time B in format "00:00:00" using awk shell script like (by just comparing the time columns of time directly) : 我想使用awk shell脚本对time Atime B之间的行进行排序,格式为"00:00:00" (通过直接比较时间的时间列):

awk '{
        if ("15:21:14" <= $1 && $1 <= "15:51:14")
        {print $0}
     }'

I got the right rows, but is it absolutely right for me to do this? 我得到了正确的行,但是这样做对我绝对正确吗?
If not, what is the right way? 如果没有,正确的方法是什么?

Assuming when you said "...I want to sort rows..." you really meant you want to SELECT rows, here's how to write that in awk: 假设当您说“ ...我要对行进行排序...”时,您的确意味着要选择行,这是在awk中编写的方法:

awk '("15:21:14" <= $1) && ($1 <= "15:51:14")' file

This will work as long as all of your timestamps follow the same format and use a full 2 digits for every number, padding single-digit hours, for example, with a leading zero. 只要您所有的时间戳都遵循相同的格式,并且每个数字都使用完整的2位数字,例如填充两位数的小时数(例如,前导零),则此方法将起作用。 If you want to sort the result then pipe the output to sort: 如果要对结果进行排序,请通过管道将输出排序:

awk '("15:21:14" <= $1) && ($1 <= "15:51:14")' file | sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM