in reply to extract lines from file between date range.

Hello rinkish85,

Part of learning how to write a script is trying and failing. Apart from that golden rule is also research on the web. If you had spend 5 minutes searching you would have found Getting lines in a file between two patterns and How do I extract all text between two keywords like start and end? that have previously asked on this forum.

Fellow Monk poj has already replied to your question but just for fun here another way. Since you do not specify if you want to capture the dates between starting and ending point in between if they are duplicates of dates, see below another possible way to do it based on your sample of data:

#!/usr/bin/perl use strict; use warnings; use IO::All; my @data; while (<>) { chomp; if (/(.*)0[1-6]\/02\/2018/s) { io('out.txt')->appendln($_); } } continue { close ARGV if eof; # Not eof()! } __END__ $ perl test.pl in.txt && cat out.txt DQ94JD84 S8G2H A9X946N 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018 XA29EN35 M4C6M D7F577Q 111.222.333.123 AWAY - [02/Feb/2018:01 +:08:39 -0800] 02/02/2018 JK20TQ67 K1L0V T6Z148X 111.222.333.123 HOME - [03/Feb/2018:01 +:08:39 -0800] 03/02/2018 SO78NZ28 B5S8J W9F920Z 111.222.333.123 HOME - [04/Feb/2018:01 +:08:39 -0800] 04/02/2018 BI55SY64 R6P5H A9U757R 111.222.333.123 HOME - [05/Feb/2018:01 +:08:39 -0800] 05/02/2018 MH72RG27 Y6X0N C7E352J 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018 ET43US76 F5S3W X2L870O 111.222.333.123 HOME - [06/Feb/2018:01 +:08:39 -0800] 06/02/2018 TJ47EG77 W5J6A A7L557Q 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018

The code provides you the ability to insert multiple files through the @ARGV.

Update: In case you do not want to include also the starting point and the ending point simply increment the date in regex by one unit on the start and decrease by one on the end. This way it will not include start and end.

Sample below:

#!/usr/bin/perl use strict; use warnings; use IO::All; my @data; while (<>) { chomp; if (/(.*)0[2-5]\/02\/2018/s) { io('out.txt')->appendln($_); } } continue { close ARGV if eof; # Not eof()! } __END__ $ perl test.pl in.txt && cat out.txt XA29EN35 M4C6M D7F577Q 111.222.333.123 AWAY - [02/Feb/2018:01 +:08:39 -0800] 02/02/2018 JK20TQ67 K1L0V T6Z148X 111.222.333.123 HOME - [03/Feb/2018:01 +:08:39 -0800] 03/02/2018 SO78NZ28 B5S8J W9F920Z 111.222.333.123 HOME - [04/Feb/2018:01 +:08:39 -0800] 04/02/2018 BI55SY64 R6P5H A9U757R 111.222.333.123 HOME - [05/Feb/2018:01 +:08:39 -0800] 05/02/2018

Update2: Reducing the lines a bit. See below:

#!/usr/bin/perl use strict; use warnings; use IO::All; my @data; while (<>) { chomp; io('out.txt')->appendln($_) if (/(.*)0[2-5]\/02\/2018/s); } continue { close ARGV if eof; # Not eof()! } __END__ $ perl test.pl in.txt && cat out.txt XA29EN35 M4C6M D7F577Q 111.222.333.123 AWAY - [02/Feb/2018:01 +:08:39 -0800] 02/02/2018 JK20TQ67 K1L0V T6Z148X 111.222.333.123 HOME - [03/Feb/2018:01 +:08:39 -0800] 03/02/2018 SO78NZ28 B5S8J W9F920Z 111.222.333.123 HOME - [04/Feb/2018:01 +:08:39 -0800] 04/02/2018 BI55SY64 R6P5H A9U757R 111.222.333.123 HOME - [05/Feb/2018:01 +:08:39 -0800] 05/02/2018

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!