Problem with KSH script using scalar variable

Hi Guys,

I'm having a hard time bringing out my desired output from my korn shell script. This particular statement from my script its seems not working perl -ne 'print if $_ lt ${date1}' . My complete script as shown below. Please help.

Code:

#!/usr/bin/ksh

HOME=/export/home/REPORTS/Stats
BIN=${HOME}/bin
OUT=${HOME}/out
date1=`date '+20%y-%m-%d'`
hostname=`hostname`

work1=`cat file.txt | grep -i ' 2 1' | nawk '{print $4}' | perl -ne 'print if $_ lt ${date1}' > ${OUT}/report.txt`

input file:
file.txt

Jason         2         1               2009-11-21
Richard       7         1               2011-03-29
Mark          2         1               2010-10-02
Michael      14         1               2010-08-23
Nora         15         1               2010-06-23
Beth          2         1               2008-02-06
Nick          2         1               2010-12-09
Danny         2         1               2010-10-27
Dexter       30         1               2010-05-07
Garry         2         1               2011-06-14
Allan         2         1               2010-02-26
Ben          39         1               2010-03-15

Now I'm expecting output like this below.

output file:
report.txt

2009-11-21
2010-10-02
2008-02-06
2010-02-26

Thanks in advance.

Br,
victorneri

Don't forget to use code tags.

If you need display full year, use this command:

date '+%Y-%m-%d'

2010-09-18

Last line can be replaced by one awk:

work1=`nawk -v d=$date1 '$2==2&&$3==1&&$4<d{print $4}' file.txt > ${OUT}/report.txt`

Hi rdcwayx,

Thanks for the reply. But the code you gave is still not working. The statement in your code seems you are comparing ' 2 1 date' versus $date1.

What I want to compare is only the date on $4 having ' 2 1' versus the date today ($date1). And not the entire ' 2 1 date'. Thanks.

Best Regards.

---------- Post updated at 01:11 PM ---------- Previous update was at 12:46 PM ----------

Hi rdcwayx,

Just to let you that is perfectly working now. You just gave me the idea on the date code (date '+%Y-%m-%d') and the nawk -v d=$date1. It works for me.

Thanks mate!

Great to be member here! :b: Cheers!

Br,
victorneri

---------- Post updated at 01:31 PM ---------- Previous update was at 01:11 PM ----------

I forgot to attach the code.

work1=`cat file.txt | grep -i ' 2 1' | nawk -v d=$ngayon '$4 <= d {print $4}' > ${OUT}/report.txt`

Thanks gain.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:31 PM ----------

Hi,

Now I want to make it a loop on an array. From my input file, the array will be the CLASS (2, 7, 14, 15, 30, 39) column. Please help me the code on how to write a loop that performs an operation on every element of an array.

#!/usr/bin/ksh

HOME=/export/home/REPORTS/Stats
BIN=${HOME}/bin
OUT=${HOME}/out
date1=`date '+20%y-%m-%d'`
hostname=`hostname`

work1=`cat file.txt | grep -i ' 2 1' | nawk -v d=$ngayon '$4 <= d {print $4}' > ${OUT}/report.txt`

Input file:

NAME        CLASS      SEC           DATE         
Jason         2         1         2009-11-21
Richard       7         1         2011-03-29
Mark          2         1         2010-10-02
Michael      14         1         2010-08-23
Nora         15         1         2010-06-23
Beth          2         1         2008-02-06
Nick          2         1         2010-12-09
Danny         2         1         2010-10-27
Dexter       30         1         2010-05-07
Garry         2         1         2011-06-14
Allan         2         1         2010-02-26
Ben          39         1         2010-03-15

Thanks in advance.

Br,
victorneri

First, cat is useless.

Second, back to the nawk command, if you need set CLASS as var, then just set it.

for class in 2 7 14 15 30 39
do
  work1=`nawk -v d=$date1 -v c=$class '$2==c&&$3==1&&$4<d{print $4}' file.txt > ${OUT}/report.txt`
done
$ ruby -F"\s+" -ane 'BEGIN{t=Time.now;y=t.year;m="%02d"%t.month;d="%02d"%t.day};print if $F[1]+$F[2]=="21" and $F[3]<"#{y}-#{m}-#{d}" ' file
Jason         2         1               2009-11-21
Beth          2         1               2008-02-06
Allan         2         1               2010-02-26

Hi rdcwayx,

I think your code is OK. But as I've said the statement in your code seems you are comparing ' 2 1 date' versus $date1. What I want to compare is only the date on $4 having ' 2 1' versus the date today ($date1). And not the entire ' 2 1 date'. Thanks.

Br,
victorneri

---------- Post updated at 03:27 PM ---------- Previous update was at 03:02 PM ----------

Hi rdcwayx,

Sorry but this is my actual input file. They are in comma separated by the way. I think your code is correct but please re-consider the comma (,) in your code. Thanks again.

Input file:

NAME,CLASS,SEC,DATE         
Jason,2,1,2009-11-21
Richard,7,1,2011-03-29
Mark,2,1,2010-10-02
Michael,14,1,2010-08-23
Nora,15,1,2010-06-23
Beth,2,1,2008-02-06
Nick,2,1,2010-12-09
Danny,2,1,2010-10-27
Dexter,30,1,2010-05-07
Garry,2,1,2011-06-14
Allan,2,1,2010-02-26
Ben,39,1,2010-03-15