Operating System - HP-UX
1752805 Members
5541 Online
108789 Solutions
New Discussion юеВ

Re: please help with kshell script..

 
SOLVED
Go to solution
amonamon
Regular Advisor

Re: please help with kshell script..

yes number 2 is correct but year is not relevant as well as month..just day..but that is in point 2.

In point 1..
if I have in my file line like this:
....
..
2222228|2007|2|28
5656569|2007|1|31
...
..
and today is 31 of march 2007 I want both lines to be in my result file.

or if I have line like this:

....
..
2123221|2007|8|31
5633561|2007|9|30
...
..

and today is for example 30 november 2007 (last day in november)

then both lines should be in result file..
I tryed with some greps..but if does not results way I want..

THANK U for patience..

cheers,

Peter Nikitka
Honored Contributor

Re: please help with kshell script..

Hi,

your original request can be done easy. You have to keep in mind to escape the pipe symbol when it's NOT used as a syntax element (OR) in a pattern definition:

awk -v ym=$(date '+%Y|%m|%d') -F'|' 'BEGIN {split(ym,z,"|");lookfor="\\|"z[3]"$"}
$0 ~ lookfor {if($(NF-1) != z[2]) print}' inputfile

Your extended request regarding the last day of a month requires much more work.
Let's see, if I find it interesting enough to formulate it ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor
Solution

Re: please help with kshell script..

OK,

its your turn to test this :-)

- I contruct a RE which is created+set dynamically to the date input.
- You have take the year into account, because of the leap year computation for february.
- To get no output for the current month at the last day of a month is left as an exercise.


awk -v ym=$(date '+%Y|%m|%d') 'BEGIN {split(ym,z,"|");stdlook="\\|"z[3]"$"
lof[1]=31;
if(z[1]%4) lof[2]=28
else lof[2]=29
lof[3]=31
lof[4]=30
lof[5]=31
lof[6]=30
lof[7]=31
lof[8]=31
lof[9]=30
lof[10]=31
lof[11]=30
lof[12]=31
lmonstr="\\|1\\|31$"
for(i=2;i<13;i++) lmonstr=lmonstr"|\\|"i"\\|"lof[i]"$"

print "lmonstr: ",lmonstr

if(z[3]==lof[z[2]]) lookup=lmonstr
else lookup=stdlook
}
$0 ~ lookup' inputfile

mfG Peter

The test the last month computation, use
awk -v ym='2006|12|31' ...

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
john korterman
Honored Contributor

Re: please help with kshell script..

Hi,

try this, using your input file as $1...


#!/usr/bin/sh

LAST_DAY_OF_MONTH=no
THIS_MONTH=$(date +%m)
THIS_DAY=$(date +%d)
MONTH=$(echo ${THIS_MONTH#0})
DAY=$(echo ${THIS_DAY#0})

case $THIS_MONTH in
1|3|5|7|8|10|12)
if [ "$THIS_DAY" = "31" ]
then
LAST_DAY_OF_MONTH=yes
fi
;;
4|6|9|11)
if [ "$THIS_DAY" = "30" ]
then
LAST_DAY_OF_MONTH=yes
fi
;;
2)
;;
4|6|9|11)
if [ "$THIS_DAY" = "30" ]
then
LAST_DAY_OF_MONTH=yes
fi
;;
2)
if [ "$THIS_DAY" = "28" -o "$THIS_DAY" = "29" ]
then
LAST_DAY_OF_MONTH=yes
fi
;;
esac
if [ "$LAST_DAY_OF_MONTH" = "yes" ]
then
grep -E "\|2\|28$|\|1\31$|\|3\|31$|\|4\|30$|\|5\|31$|\|6\|30$|\|7\|31$|\
|8\|31$|\|9\|30$|10\|31$|11\|30$|12\|31$" $1
else
grep "|${THIS_DAY}"$ $1
fi


regards,
John K.
it would be nice if you always got a second chance
amonamon
Regular Advisor

Re: please help with kshell script..

thanks this looks OK but I have problems with this grep -E

I got this:

grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
john korterman
Honored Contributor

Re: please help with kshell script..

Hi again,

if your system does not like "grep -E", try using a number of "grep -e"s instead, e.g.:

grep -e "\|2\|28"$ -e "|\|1\31"$ -e "|\|3\|31"$ -e "|\|4\|30"$ -e "|\|5\
|31"$ -e "|\|6\|30"$ -e "|\|7\|31"$ -e "|\|8\|31"$ "|\|9\|30"$ "|10\|31"$ -e "|1
1\|30"$ -e "|12\|31$" $1


regards,
John K.
it would be nice if you always got a second chance