Need Help Writing Unix Script - URGENTLY

I need help writing a Unix Script, using Korn Shell (#!/usr/bin/ksh).

Basically, the script reads a file with the filename CAL_yyyymmdd  (yyyymmdd changes everyday).

In the CAL_yyyymmdd file, the script needs to skip the first line of text and starts reading the second line. The script should first check the third column (which states C or F).

If the second line, third column reads a C, then it takes the date from the second column from the right (missing out the C), the value 20090220 and it puts this value into a file called tom.dat.

If the script reads the second line, third column and finds its a F, it then continues to move on to the next line, third column.

When it reads the next line, third column and finds a C, the script should take the date from the second column, from the right (missing out the C), take the value 20090223 and write it to a file called dayaftertom.dat.

Once the script has read the second C from the third column, it should quit. This script is to take tomorrow's date and the date of the day-after-tomorrow and write it out to a file.

I've attached the file from which the Script needs to read its data from.

011060                8A20090220160040                      
011100                852009022016004020090220C C                                          120090225
011100                852009022016004020090221C F                                          000000000
011100                852009022016004020090222C F                                          000000000
011100                852009022016004020090223C C                                          120090226
011100                852009022016004020090224C C                                          120090227
011100                852009022016004020090225C C                                          120090302

Who is Participating?
OK, that's clarified and simplied things.

FILE=CAL_`date +%Y%m%d`
awk '$3=="C"{print substr($2,17,8)}' $FILE | head -1 >tom.dat
awk '$3=="C"{print substr($2,17,8)}' $FILE | sed -n 2p >dayaftertom.dat

Open in new window

so we want the first line that contains a C in the 3rd column?

awk '$3=="C"{print substr($2,17,8);exit}' CAL_`date +%Y%m%d` > tom.dat
Your specifications are a little unclear.

According to your specs, if the second line, 3rd column contains an F then skip it until the 3rd column contains a C and write the date to dayaftertom.dat.  Does the 2nd occurance of C also get written to dayaftertom.dat?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

ben1211Author Commented:
Tintin....the first occurance of C from the 3rd column will take the date from the second column from point 17-24 (20090220) and write it to file tom.dat.  

{Note tht 17-24 is from the string of characters from the second column 852009022016004020090220C. So counting from left to right the date 20090220, will start from point 17-24)

The second occurance of C is then checked from the 3rd Column. Once this criteria is met, it read the second column, reading position 17-24 and writing the value to dayaftertom.dat.

Ozo...could you explain your script please? I do not want to merely just copy, but I would like to learn what its doing.
ben1211Author Commented:
Ozo....can't seem to run your script. Not getting any result at all.
did you look in tom.dat?
did   the CAL_`date +%Y%m%d` file for today contain what you posted?
ben1211Author Commented:
Tintin....I need the script to read yesterday's date
> .. I need the script to read yesterday's date
and in your question you stated:
> This script is to take tomorrow's date ..

Could you come over with an unambigious description.
ben1211Author Commented:
My apologies. I made a mistake. It has to be yesterday's date. I'm sorry I didn't reply sooner.
awk '((NR>1)&&($3=="C")&&(c<2)){c++;print substr($2,17,8)}'  CAL_`env TZ=GMT+24 date +%Y%m%d`
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.