Operating System - HP-UX
1753486 Members
4423 Online
108794 Solutions
New Discussion юеВ

Re: please help with kshell script..

 
SOLVED
Go to solution
amonamon
Regular Advisor

please help with kshell script..

hello I have file like this:


1234561|2007|2|28
4567891|2007|3|31
7897891|2007|4|5
9879871|2007|5|30
2578961|2007|6|25
...
..
.

where $2 is year $3 is month and $4 is day..

considering if today is 30may2007 I heed to have on my other file just one line:

9879871|2007|5|30

is that possible to bo done in ksh pleasee..


thanks a lot...
15 REPLIES 15
Peter Godron
Honored Contributor

Re: please help with kshell script..

Hi,
how about:

grep -e '|2007|5|30' datafile

Peter Godron
Honored Contributor

Re: please help with kshell script..

Hi (again),
or for a more generic solution:

#!/usr/bin/ksh
grep -e "$1|$2|$3" datafile

$ ./b.sh 2007 5 30
9879871|2007|5|30
john korterman
Honored Contributor

Re: please help with kshell script..

Hi,

$ cat ./extract.sh

#!/usr/bin/sh
if [ $# != 4 ]
then
THIS_YEAR=$(date +%Y)
THIS_MONTH=$(date +%m)
THIS_DAY=$(date +%d)
MONTH=$(echo ${THIS_MONTH#0})
DAY=$(echo ${THIS_DAY#0})
grep "$THIS_YEAR|$MONTH|$DAY" $1
else
grep "$1|$2|$3" $4
fi


Try running it like this:

$ ./extract.sh 2007 2 28 ./infile
1234561|2007|2|28

or wait till the end of February and run it without parameters...


regards,
John K.
it would be nice if you always got a second chance
SANTOSH S. MHASKAR
Trusted Contributor

Re: please help with kshell script..

Hi,

I assume u want to get only the line corresponding to today's date
in other file.

If so then try this

$ awk -F \| -v a=`date "+%Y"` -v b=`date "+%m"` -v c=`date "+%d"` '$2==a&&$3==b&&$4==c {print $1}' >

Thats all

-Santosh
SANTOSH S. MHASKAR
Trusted Contributor

Re: please help with kshell script..

Hi,

Sorry for a small mistake,

Pl. replace {print $1} with {print $0}

-Santosh
amonamon
Regular Advisor

Re: please help with kshell script..

Yes I need to have today's date and then if in file I do have some lines like this:

1111119|2007|3|31


if dodays date is april 30 I need to take that line as well as a result..

for example: today is 28 feb 2007 I need to test next date..if next date is 1.something.2007 I need to have
that line as result as well..

for example if I have:
1111119|2007|2|28
1111119|2007|4|30
1111119|2007|12|31
1111119|2007|8|8

and let assume that today is june 30 2007 as an output I should have:

1111119|2007|2|28
1111119|2007|4|30
1111119|2007|12|31
Patrick Wallek
Honored Contributor

Re: please help with kshell script..

I am thoroughtly confused as to what you are looking for as output.

Can you please state your EXACT requirements?
amonamon
Regular Advisor

Re: please help with kshell script..

yes I know it is tricky..little..

but if today is for example june 30, 2007

and my input file is like this:

8111319|2007|2|28
1112119|2007|4|30
7111119|2007|12|31
1231119|2007|8|8

script should check if next day is 1st in the month and output this:

8111319|2007|2|28
1112119|2007|4|30
7111119|2007|12|31

becouse february has 28 days...if today is for example 8 sept or 8 dec or any other month output should be like this:

1231119|2007|8|8

that means if I have bigger file:

1234561|2007|2|28
4567891|2007|3|31
7897891|2007|4|5
9879871|2007|5|30
1578961|2007|6|28
1897893|2007|10|28
7879871|2007|12|30
9578962|2007|12|28

and today is 28 of august:

output should be like this:

1234561|2007|2|28
1578961|2007|6|28
1897893|2007|10|28
9578962|2007|12|28

that means does not metter which month is it looks fot day..


Patrick Wallek
Honored Contributor

Re: please help with kshell script..

OK, so let me try to get this straight. Your requirements are:

1) If the current day is the LAST DAY of the month, then look in the file for ANY LINES from the current year that occur on the last day of their respective months.

2) If the current day is some other time in the month, then look for lines from the current year from any month where the day equals today.

Is that accurate?