Merging lines - Tuning the script

Hi, Anybody can tune the below script? Its taking 1hour to process 7000 lines.

addendaRecInd=7
entryDetailRecord=6
hdrDetailRecord=1
companyHdrRecord=5
companyCtrlRecord=8
trlRecord=9
recLength=94

FormatDone=false
trlCompleted=false

prevEntryCalc=false
batchCnt=0
hdrFound=false
while read line
do

# Find the Line #
grep -n "$line" $srcFile |cut -d : -f1 |read lineno
#find the Rec Type
if [ `echo $line|grep "^$companyHdrRecord"| wc -l` -ne 0 ]
then
hdrFound="true"
val=""
typeset -L94 line
hdr="$line"
typeset -R0 line
typeset -R0 batchCnt
fi
if [ `echo $line|grep "^$hdrDetailRecord"| wc -l` -ne 0 ]
then
typeset -L94 line
value=""
echo "$line" >> $destFile
line=""
typeset -R0 line
fi
if [ `echo $line|grep "^$trlRecord"| wc -l` -ne 0 -a $trlCompleted = false ]
then
echo $trlCompleted
value=""
typeset -L94 line
echo "$line" >> $destFile
line=""
typeset -R0 line
trlCompleted=true
echo $trlCompleted
fi
if [ `echo $line|grep "^$companyCtrlRecord"| wc -l` -ne 0 ]
then
if [ "$hdrFound" != "true" ]
then
echo "hdrFound" $hdrFound
echo "The last line is " $value
if [ "$value" != " " ]
then
typeset -L599 value
typeset -L6 batchCnt
echo "$value$batchCnt" >> $destFile
typeset -R0 value
typeset -R0 batchcnt
fi
typeset -L94 line
typeset -L6 batchcnt
echo "$line$batchCnt" >> $destFile
typeset -R0 line
typeset -R0 batchcnt
value=""
line=""
prevEntryCalc=false
fi
fi
if [ `echo $line|grep "^$entryDetailRecord"| wc -l` -ne 0 ]
then
# Print Header after the entry detail record has been read
if [ "$hdrFound" != "false" ]
then
batchCnt=`expr $batchCnt + 1 `
typeset -L94 hdr
typeset -L6 batchCnt
echo "$hdr$batchCnt" >> $destFile
typeset -R0 hdr
hdrFound="false"
hdr=""
fi
if [ "$prevEntryCalc" != "false" ]
then
typeset -L599 value
typeset -L6 batchCnt
echo "$value$batchCnt" >> $destFile
typeset -R0 value
typeset -R0 batchcnt
value=""
fi
line=`echo "$line"`
value=`echo "$line"`
prevEntryCalc="true"
fi
if [ `echo $line|grep "^$addendaRecInd" | wc -l` -ne 0 ]
then
typeset -L101 line
len=`echo $line|wc -c`
#echo "The Length of addenda record is "$len
value="$value$line"
typeset -R0 line
fi
done < $srcFile

The example of the file format is i am using # for comment

101 10000217311 # Header always starts with 1
5200Paydonee 11 # Batch always Header Stats with 5
622100002173011 # Data always starts with 6
705CTB 4 0112 # If lin strts with 7, it has to be merged with previous line. and also the batch header counter has to be padded at end of line.
705Pymt Ref 4991
622100002173066
705CTB 19 5622
705532119P 386
622100002173011
705CTB 20 338
705 44888
822000000112998 # batch trailer always starts with 8
900001100021000 # Trailer of the file always starts with 9

Lines start with 7 has to be merged with the previous lines start with 6 and the line length should be 605.

Hi,

You'd better tell us your requirement. Maybe some one can figure out some different solution, then you can compare them.

I think nobody like to read the complex scripte line by line.

:stuck_out_tongue:

Thank you very much summer_cherry. Thanks for another awk reply also.

My requirement is :

i have file which has the following format

header
BatchHeader-1
Details1
700000023
Details2
75858585
72323232
BatchTrailer-1
BatchHeader-2
Details1
700000023
Details2
75858585
72323232
BatchTrailer-2
Trailer

I want the output like below.

header
BatchHeader-1
Details1700000023 1 # this 1 means that detail belongs to batchheader-1
Details27585858572323232 1
BatchTrailer-1
BatchHeader-2
Details1700000023 2 # this 2 means that detail belongs to batchheader-2
Details27585858572323232 2
BatchTrailer-2
Trailer