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!

In reply to Re: extract lines from file between date range. by thanos1983
in thread extract lines from file between date range. by rinkish85

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.