select the lines in between some time span

Hi Everyone !

i want to take all the lines from a file that falls in between some date... and every line in a file has a time stamp..

---some text---- 01/Jan/2010 ---- some other text ----
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----
---some text---- 02/Jan/2011 ---- some other text ----

suppose i want to select the lines that satisfies date between 09/Jan/2010 and 01/Jan/2011

result:

---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----

how to collect those lines from a log file?

so far i'm able to do this

current_date = date +%d/%b/%Y
Given_date = date -d "-3000 day" +%/d/%b/%Y

i'll get some time span

upper limit = 20/Oct/2011
Lower limit = 25/May/2001

i want to select the lines in which lines falls in between 20/Oct/2011 and 25/May/2001

Regards,

Ewa

---------- Post updated at 08:10 AM ---------- Previous update was at 07:36 AM ----------

a function or line of code which i can put in while loop

while read line
do
result = if satisfy the condition
result>>newfile

done<logfile

#!/usr/bin/ksh
typeset -i mI
set -A mMon n/a Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
mFrom='20100105'
mTo='20110101'
while read mLine; do
  echo ${mLine} | sed 's#.*\(..\)/\(...\)/\(....\).*#\1 \2 \3#' | read mDD mMMM mYYYY
  mI=1
  while [[ ${mI} -le 12 ]]; do
    if [[ "${mMon[$mI]}" = "${mMMM}" ]]; then
      break
    fi
    mI=${mI}+1
  done
  if [[ ${mI} -lt 10 ]]; then
    mYYYYMMDD=${mYYYY}'0'${mI}${mDD}
  else
    mYYYYMMDD=${mYYYY}${mI}${mDD}
  fi
  if [[ ${mYYYYMMDD} -ge ${mFrom} && ${mYYYYMMDD} -le ${mTo} ]]; then
    echo ${mLine}
  fi
done < File
1 Like

Hi

sorry i didn't mention in the beginning that i'm using BOURNE SHELL (bash)? it wouldn't work i guess

BR

Ewa

Hi me_newbie,

A possible solution using 'perl':

$ cat infile 
---some text---- 01/Jan/2010 ---- some other text ----
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----
---some text---- 02/Jan/2011 ---- some other text ----
$ cat script.pl
use warnings;
use strict;
use POSIX;

my %TAB_MONTH = ( 
        jan     =>      0,
        feb     =>      1,
        mar     =>      2,
        apr     =>      3,
        may     =>      4,
        jun     =>      5,
        jul     =>      6,
        aug     =>      7,
        sep     =>      8,
        oct     =>      9,
        nov     =>      10,
        dec     =>      11
);

die qq[Usage: perl $0 input-file date-from date-to\n] unless @ARGV == 3;

my $utc_date_to = date_str_to_utc( pop @ARGV ) or die qq[Bad format of date (dd/mmm/yyyy)\n];
my $utc_date_from = date_str_to_utc( pop @ARGV ) or die qq[Bad format of date (dd/mmm/yyyy)\n];

while ( <> ) {
        next if m/\A\s*\z/;
        chomp;
        my $utc_date_line = date_str_to_utc( $_ ) or next; 
        if ( $utc_date_line >= $utc_date_from && $utc_date_line <= $utc_date_to ) {
                printf "%s\n", $_;
        }
}


sub date_str_to_utc {
        my ($date_str) = $_[0];

        my ($mday,$month,$year) = $date_str =~ m|\b(\d{2})/(\w{3})/(\d{4})| or return;
        my $utc_date = POSIX::mktime( 0, 0, 0, $mday, $TAB_MONTH{ lc $month }, $year - 1900 );

        return $utc_date;
}
$ perl script.pl infile "09/Jan/2010" "01/Jan/2011"
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----

Regards,
Birei

1 Like