Operating System - HP-UX
1823178 Members
3769 Online
109647 Solutions
New Discussion юеВ

Re: striping a file inside of a ksh script

 
SOLVED
Go to solution
fg_1
Trusted Contributor

striping a file inside of a ksh script

Hello all

I am trying to write a ksh script here and could use some professional help (aside from the psychiatric stuff).

First the script i am writing is executing a command which produces an output file, what I would like to do with the output file is listed below:

1) Take the file and then utilize only the lines that contain the current date and the previous days date. Whatever is done with the other lines is ok since they are not needed.

2) Next I would like to take that output file and then wrap it in HTML code.

The script is listed here:


#!/usr/bin/ksh -v
#
# This script is being developed to provide a detailed report of the previous day's backup
# jobs from netbackup. This script will be executed daily at 10:00AM.
# Upon successful completion, the output from this script will be webified using html and then
# emailed to the it_backup_admins mailing list.
#
# This script must be executed utlizing the root user account.

# Environmental section

set -u

BASEDIR=/home/fgrosb01
DATE=`date +%Y%m%d`
SCRIPT_REVISION="HPUX-b.11.00.01"
SCRIPT_HPUX_VERSION=$(uname -r)
SCRIPT_SYSTEM=$(uname - n)
SCRIPT_USER=$(whoami)
SCRIPT_EXECUTION_TIME=$(date +'%Y/%m/%d/%H:%M:%S')

echo ${SCRIPT_SYSTEM} \(Version ${SCRIPT_REVISION}\) ${SCRIPT_EXECUTION_TIME}
echo _____________________________________________________________________
echo

if [ ${SCRIPT_USER} != "root" ] ; then
echo
echo "This program must be executed utilizing the root logon id only"
echo
return 501
fi
echo _______________________________________________________________________
###########################################################################
#
# This section of the script will execute the bpdbjobs command utilizing
# the file /home/fgrosb01/.xbpmonrcfg as it's configuration file for the
# output.

bpdbjobs -header -report -format $BASEDIR/.xbpmonrcfg > /home/fgrosb01/$DATE.bpdbjobs.output


I am attaching a copy of the output file as well.

Thank you in advance. Points will be awarded.
fg.
10 REPLIES 10
Sean OB_1
Honored Contributor

Re: striping a file inside of a ksh script

Set a variable to the date using the date command: "date -u %m/%d/%Y"

Do the same for yesterdays date.

Then 'grep' for those dates in the output file and put it into a tmp file.

Grep for todays date.

grep $TODAYSDATE inputfile > /tmp/outputfile
grep $YESTERDAYSDATE inputfile >> /tmp/outputfile

Sort the file if needed. man sort.

Then cat the html header into a file, concat backup output, and concat html footer into file.
harry d brown jr
Honored Contributor

Re: striping a file inside of a ksh script

I'm going to "steal" Sean's variables:

cat inputfile | grep -e "08/17/2002" -e "08/18/2002" | awk '{printf("

\n%s\n",$0);}'

live free or die
harry

Live Free or Die
Sridhar Bhaskarla
Honored Contributor
Solution

Re: striping a file inside of a ksh script

Hi Frank,

The issue is getting yesterday's date.

I will keep a stamp file to get yesterday's date. Or you can use Clay's Caljd.sh script (search in the forums) to determine the date of yesterday.

To use my method, you will first need to touch a file yesterday.stamp with current yesterday's date in it. I will just modify your script a little bit.

BASEDIR=/home/fgrosb01
STAMP=$BASEDIR/yesterday.stamp
RESULT=$BASEDIR/result
DATE=`date +%Y%m%d`
Y_DATE=`cat $STAMP`
SCRIPT_REVISION="HPUX-b.11.00.01"
SCRIPT_HPUX_VERSION=$(uname -r)
SCRIPT_SYSTEM=$(uname - n)
SCRIPT_USER=$(whoami)
SCRIPT_EXECUTION_TIME=$(date +'%Y/%m/%d/%H:%M:%S')



echo ${SCRIPT_SYSTEM} \(Version ${SCRIPT_REVISION}\) ${SCRIPT_EXECUTION_TIME}
echo _____________________________________________________________________
echo

if [ ${SCRIPT_USER} != "root" ] ; then
echo
echo "This program must be executed utilizing the root logon id only"
echo
return 501
else
grep $Y_DATE $OUTPUT > ${RESULT}.${DATE}

cat << EOF > ${RESULT}.${DATE}.html




EOF
cat ${RESULT}.${DATE} >> ${RESULT}.${DATE}.html
cat << EOF >> ${RESULT}.${DATE}.html





fi

echo $DATE > $STAMP
echo _______________________________________________________________________
###########################################################################



-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: striping a file inside of a ksh script

Frank.. Forgot an "EOF" after .

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
fg_1
Trusted Contributor

Re: striping a file inside of a ksh script

Thank you for the advice so far guys, i am still evaluating everything and trying to take them in.

I have also been trying to give thought to striping out certain fields to create another file, kind of seperating some columns from the main output, can any of you provide me with an idea on how that would be done.
Ex: take the jobid, jobstatus, start time, end time and make a new file with those fields.

I appreciate the help on this.
fg
harry d brown jr
Honored Contributor

Re: striping a file inside of a ksh script

#!/usr/bin/ksh
cat /tmp/inputfile | awk '{
jobid=substr($0,1,10);
jobtype=substr($0,14,11);
jobstate=substr($0,23,8);
jobstatus=substr($0,31,9);
jobclass=substr($0,40,28);
jobschedule=substr($0,70,19);
jobclient=substr($0,89,26);
jobserver=substr($0,116,26);
jobstarted=substr($0,143,19);
jobelapsed=substr($0,169,14);
jobended=substr($0,180,19)
jobKbytes=substr($0,199,14);
jobfiles=substr($0,213,14);
jobestimated=substr($0,227,7);
printf("%s_%s_%s_%s_%s_%s_%s_%s_%s_%s_\n",jobid,jobtype,jobschedule,jobclient,jo
bserver,jobstarted,jobended,jobKbytes,jobfiles,jobestimated);
}'


live free or die
harry
Live Free or Die
fg_1
Trusted Contributor

Re: striping a file inside of a ksh script

Harry

That looks interesting, can you explain to me what that is actually doing since i am not too proficient with awk.

TY.
harry d brown jr
Honored Contributor

Re: striping a file inside of a ksh script

Frank,

the script is cat'ing the file to awk. "awk", when it receives data from a pipe (see the cat to awk below), it places each separate line in variable $0 (dollar-sign zero), much like the shell's do.

#!/usr/bin/ksh
cat /tmp/inputfile | awk '{

# and here we are "parsing" the fields
# based upon their position within
# the file:

jobid=substr($0,1,10);

#
# the job ID (above) starts in position 1 and is 10 characters long
#

jobtype=substr($0,14,11);

#
# the job type (above) starts in column 14 and is 11 charcaters long
#

# the $0 is the whole line

jobstate=substr($0,23,8);
jobstatus=substr($0,31,9);
jobclass=substr($0,40,28);
jobschedule=substr($0,70,19);
jobclient=substr($0,89,26);
jobserver=substr($0,116,26);
jobstarted=substr($0,143,19);
jobelapsed=substr($0,169,14);
jobended=substr($0,180,19)
jobKbytes=substr($0,199,14);
jobfiles=substr($0,213,14);
jobestimated=substr($0,227,7);

#
# here I am just outputing some variables with an
# underscore (_) just to make sure I have the fields
# aligned.
#

printf("%s_%s_%s_%s_%s_%s_%s_%s_%s_%s_\n", jobid, jobtype, jobschedule, jobclient, jobserver, jobstarted, jobended, jobKbytes, jobfiles, jobestimated);

}'

The %s in the print statement is a string identifier.

Just take the fields you want, and put them into a print statemnet like the one above. Make sure you have an %s (percent ess) for each field being outputed.



live free or die
harry
Live Free or Die
fg_1
Trusted Contributor

Re: striping a file inside of a ksh script

Harry

Your AWESOME. Thank you very much for that piece. It's an awesome idea.

FG
harry d brown jr
Honored Contributor

Re: striping a file inside of a ksh script

Frank,

Of course the same can be done in perl, and almost exactly as it's done in awk.

live free or die
harry
Live Free or Die