Operating System - HP-UX
1833792 Members
2161 Online
110063 Solutions
New Discussion

Need help for scripting..

 
SOLVED
Go to solution
Sailesh_1
Advisor

Need help for scripting..

Hi,

I would need to add a trailer record into a file generated by a script. The trailer record format should be "TRAILERDDMMYYYYNNNNNN"
where DD=current date, MM=current Month, YYYY=current year and NNNNNN=no of records in the file (excluding the trailor record).

For example, for a file created on November 17, 2003 with 645 records, the trailor should read as TRAILER17112003000645

Now, As you see in the example, the number of records could be any thing from 1 to 999999.

What should I do to create the trailor record in the proper format to fill all the 6 record fields. i.e, if the number of record is 589, then it should read as 000589

Appreciate your help!

Thanks,
Sailesh
7 REPLIES 7
Jannik
Honored Contributor

Re: Need help for scripting..

seqid=`date -u +%d%m%Y`
filename=TRAILER$seqid
I don't know what you meen with records. Could you please clarify!
jaton
Steven E. Protter
Exalted Contributor

Re: Need help for scripting..

You may find the following date converstion script useful in your scriptng.

http://www.hpux.ws/merijn/caljd.sh

A. Clay Stephenson Wrote it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Poff
Honored Contributor

Re: Need help for scripting..

Hi,

Something like this might work. I wrote a little shell script named trail.sh. Just pass it the filename to generate the trailer record for as a parameter. The 'somefile' I used has ten lines in it:

>cat trail.sh
#!/bin/sh

# trail.sh

MYFILE=$1
typeset -Z6 LINES
MYLINES=$(wc -l $MYFILE | awk '{print $1}')
MYDATE=$(date '+%d%m%Y')

echo "TRAILER${MYDATE}${MYLINES}"


>./trail.sh somefile
TRAILER19112003000010


The trick to formatting your record count is to use the 'typeset' command. The -Z6 option tells typeset that the MYLINES variable will be six numeric characters, zero filled. You can read up on typeset in the sh-posix man page.

Have fun!

JP



Sailesh_1
Advisor

Re: Need help for scripting..

Thanks for the reply.
Records are something like the number of entries in the file. simply

$ cat file_name |wc -l

There are 6 feilds allotted for the records.
and I need to fill all the six fields.even if the number is 589, the trailor should looks like TRAILOR`date`000589. My question is how do I add those zeros in it?

i.e, if the record is 589, then i need to add 3 zeros before 589. and if the record is 1589. then i need to add 2 zeros before 1589 to fill the vacant.

Thanks,
Sailesh
Mark Greene_1
Honored Contributor
Solution

Re: Need help for scripting..

once you have the record count, you can format it with awk:


RECORD_COUNT= `echo $RECORDS| awk ' { printf "%06d", $1 }'`

where $RECORDS is the unformatted count, and RECORD_COUNT would be the formatted number. So if $RECORDS = 589, $RECORD_COUNT would be 000589.

mark
the future will be a lot like now, only later
john korterman
Honored Contributor

Re: Need help for scripting..

Hi,
the easiest way is to make use of the korn shell's capacities, e.g. try this example:

#!/usr/bin/sh
typeset -Z6 NUMBER
NUMBER=$(wc -l ./infile|awk '{print $1}')
echo $NUMBER


for further information, man ksh and look for typeset.

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

Re: Need help for scripting..

Thank you Mark and all others who replied.
That works perfect!!

Thank you all once again.

Sailesh