grep a log file to filter previous dates

Hi,

I have problem of filtering a log file from my perl script.

#cat /data/pinpe.csv_20070731 | nawk -v FS=, '{print $1','$18','$22','$26}' | grep -w 100 | grep -w 1 | nawk '{print $4}'

Below is the output:

2009-06-16
2009-01-29
2009-06-02
2008-03-05
2007-08-05
2007-09-24
2007-07-10
2007-10-27
2007-10-14
2008-08-02
2007-11-10
2007-10-16
2008-09-15
2008-01-30
2008-12-08
2007-09-26
2007-11-29
2008-04-01
2007-11-21
2007-12-18
2007-10-04
2007-06-29
2008-01-13
2007-10-28
2007-10-20
2007-09-13
2008-11-05
2008-08-05
2008-04-10
2007-09-10
2008-10-12
2007-11-29
2008-04-02
2009-03-05
2009-07-18
2007-11-16
2007-10-13

Now my dilemma is I want to filter this ouput by my preferred dates. I wanted it to output the date from yesterday and backwards. In other words I want to filter all the dates less than today's date (i.e. 2007-07-30, 2007-07-29, 2007-07-28, etc. and below). And finally count the number of lines for the final output.

Tnx in advance.

Br, Pete

change your last nawk to be
nawk '{ if ( $4 < DATE ) {print $4}}' DATE=$(date '+%Y-%m-%d') -

or you could use a -v parameter to feed in the date

nawk -v DATE=$(date '+%Y-%m-%d') '{ <same code> }'

#!/bin/ksh
typeset -i mCnt=0
mYYYYMMDD=`date +"%Y-%m-%d"`
echo 'mYYYYMMDD = '$mYYYYMMDD
while read mLine
do
  if [[ "${mLine}" < "${mYYYYMMDD}" ]]; then
    echo 'Found = '${mLine}
    mCnt=${mCnt}+1
  fi
done < input_file
echo 'Total lines = '${mCnt}

Hi perl Gurus,

Here is the code in tags. I'd made it simple code for not to look more confusing.

use Net::Telnet;

@DATE=$telnet->cmd("date '+%Y-%m-%d'");
chomp($DATE[0]);

@Discon=$telnet->cmd("cat /data/pinpe.csv_20070731 | nawk '{print \$4}' | perl -ne 'print if \$_ lt @DATE[0]' | wc -l");
chomp($Discon[0]); 

print "@Discon[0]\n";

and this is the output error:

Illegal octal digit '8' at -e line 1, at end of line

Was it perl thought that I was dealing with an octal number and when he came along with 8 or 9 (becasue of 2007-08-01) which stopped it from making sense, so perl quite rightly complained? Pls help. Tnx.

Br,
Pete

Hi perl Gurus,

I just wanna make this up. Pls nedd some hand. Tnx in advance.

Br, Pete

Hi,

Someone solved my problem. A "Monk" from PerlMonks - The Monastery Gates. :b:

I just want to share it with you guys. Below is the right syntax for my code.

use Net::Telnet;
use IPadd;

$ipadd=IPadd->new();
$ipadd->ipadd1();

@Date1=$telnet->cmd("date '+%Y%m%d'");
chomp($Date1[0]);
@Date2=$telnet->cmd("date '+%Y-%m-%d'");
chomp($Date2[0]);

@Discon=$telnet->cmd("cat /data/pinpe.csv_@Date1[0] | nawk -v FS=, '{print \$1','\$18','\$22','\$26}' | grep -w $array1[$count-1] | grep -w 1 | nawk '{print \$4}' | perl -ne 'print if \$_ lt \"@Date2[0]\"' | wc -l");

The key to my problem is 'interpolation'. The code that the Perl interpreter sees is:

print 2007-08-05;

which is some arithmetic (2007 - 08 - 05) containing two octal constants and a decimal constant. The proper syntax is I need to quote the interpolated date so that the interpreter sees a string instead:

\"@Date2[0]\"

Cheers! :slight_smile:

Br, Pete