简体   繁体   中英

Linux file get lines with column matching specific datetime range

How to get lines matching datetime range. For example, get all lines between 2017-09-08 00:00:00 and 2017-09-09 00:00:00. Delimiter is space.

file:

2017-09-07 19:00:01 a
2017-09-08 20:00:01 a
2017-09-08 21:00:03 a
2017-09-10 19:00:01 a

output:

2017-09-08 20:00:01 a
2017-09-08 21:00:03 a
awk '$1FS$2> "2017-09-08 00:00:00"  && $1FS$2 <"2017-09-09 00:00:00"' inputfile
2017-09-08 20:00:01 a
2017-09-08 21:00:03 a

Or you can store the times in variables:

awk -v start="2017-09-08 00:00:00" -v end="2017-09-09 00:00:00" '$1FS$2> start  && $1FS$2 <end' inputfile
2017-09-08 20:00:01 a
2017-09-08 21:00:03 a

Explanation:

$1 means the first column $2 means the second column FS means field separator, which is by default is space. $1FS$2 means first two columns together as one entity. This was needed because there is space between your date and time column.

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