简体   繁体   中英

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 '{
        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 '("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. If you want to sort the result then pipe the output to sort:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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