Home > Blockchain >  Extract lines matching listed date & time
Extract lines matching listed date & time

Time:08-08

From a time-based data file, I'd like to extract lines matching date & time which are listed in other text file.

From this

2020-01-01 00:00:00 0.939646 117.325 89.6035
2020-01-01 00:05:00 1.06663 112.67 90.3349
2020-01-01 00:10:00 1.04123 107.835 91.2749
2020-01-01 00:15:00 1.16821 105.275 91.7174
2020-01-01 00:20:00 1.16821 104.192 91.9994
2020-01-01 00:25:00 0.956576 106.395 91.1905
... (>100000 lines)

Extract lines of these date&time

2020-01-01 00:15:00
2020-01-01 00:20:00
2020-01-01 01:25:00
2020-01-01 02:30:00
2020-01-02 12:35:00
2020-01-03 01:40:00
2020-01-06 10:45:00
... (>1500 lines)

How do I do this with awk or grep, or something else? I have some experience with awk and grep, but not a heavy user.

From a comment:

Initially I tried grep -E "^2020-01-01 00:15:00|2020-01-01 00:20:00|....|2021-12-31 00:01:55" data_base.txt, but there are too many date&time to extract. Frankly, I tried and it returns grep: out of memory. I'm googling with something like 'extract lines matching conditions/date and time/etc..', but no result yet.

CodePudding user response:

I suggest with GNU grep:

grep -f second_file.txt data_base.txt

CodePudding user response:

awk '{dt=$1 FS $2} NR==FNR{a[dt]; next} dt in a' second_file.txt data_base.txt
  • Related