I have a record file that stores the statuses of our systems by date. The script to generate it runs via cron, so the file is constantly getting longer. I wrote a script that iterated over every line to process it and this took a very long time to do. I've heard that awk is much faster at processing large text files. My problem is that I've never used it. Is it possible to use awk to get all entries within a date range? The dates are all in seconds as they were produced with date +%s. Here is an example of output that I would like to be able to quickly find data in a range. So for example, how could I get all lines where the first column is between 1344279903 and 1344280204?
1344279903 | 0 | 0 | node | 1
1344279904 | 0 | 0 | node | 2
1344279905 | 0 | 0 | node | 3
1344280202 | 0 | 0 | node | 1
1344280203 | 0 | 0 | node | 2
1344280204 | 99 | 0 | node | 3
You can use a conditional expression like so:
awk '$1 >= 1344279903 && $1 <= 1344280204 { print $0 }' data.txt
With awk?
awk -F'|' '1344279903 <= $1 && $1 <= 1344280204' file
With sed?
sed -n '/1344279903/,/1344280204/p' file
You can make the awk expression even more efficient by explicitly exiting after the last print statement:
awk -F'|' '1344279903 <= $1 && $1 <= 1344280204{ print $0; } $1 == 1344280204{ exit; }' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With