awk script

Current field length : 361 bytes

To the record , i am appending the filename and hard-coding batchid.

The record length has increased from 361 to 400 and since i will continue to get both files ie 361 and 400 length sizes
i want to write a program that can handle both of them.

To the new files i want to cut the string from 362 to 400 and append after the batch id and filename as shown below

Both Input and Output are fixed length files

Output File Layout
------------------
'1 - 361''filename''batch_id'

'1- 361''filename''batch_id''362-400'

Existing code
-------------

for file in /export/home/input/temp/abc[0-9][0-9].dat;do

nawk '/............B[HT]0/{print "HDR/TRLR " ctr++ " : " $0 ;next }
/............H10/{print $0 |" sed s/$/'`basename $file`BATCHID'/ >>/output/outfile.dat " } ' $file

done

New Code
--------

I am not sure if this logic is correct or i might have complicated it.Please suggest

for file in /export/home/input/temp/abc[0-9][0-9].dat;do

nawk '/............B[HT]0/{print "HDR/TRLR " ctr++ " : " $0 ;next }
/............H10/{bl = substr($0,1,361);el = substr($0,362); print bl '`basename $file`BATCHID' el >>/output/outfile.dat " } ' $file

Its treating bl '`basename $file`BATCHID' el >>/output/outfile.dat " as a string rather than as a variable.

Can we run a Unix command in awk program? coz replacing SED here is giving me all kinds of problems.

Please help

Why not do something like:


$length=`cat $file | wc -c`   # use cat to get only the byte count
if [[ $length -gt 361 ]]
then
   awk '{print substr($0,0,361) FILENAME "BATCHID" substr($0,362)}' $file
else
   awk '{print $0 FILENAME "BATCHID"}' $file
fi

Modify the code to do whatever specifics you need.

Zomboo,

Please don't post duplicate posts. I have removed the duplicate post from this forum, and moved your original post to the appropriate scripting forum.

If somebody can help you, they will (and they have!) - posting in more than one forum will not get you an answer any quicker.

Thanks,
ZB

-------------------
zomboo wrote .....

Current field length : 361 bytes

-------------------------------------------

PxT wrote .....

$length=`cat $file | wc -c` # use cat to get only the byte count

-----------------------------------------

My question is "That file contains only one record ??"

If more than one record ,

$length=`head -1 $file | wc -c`