Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Improving Script Performance

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi Folks,

I have the following primitive shell script, which works cool, except its freak'n slow.

Can anyone suggest some tips to speed it up ?
Code:
#!/bin/ksh

dataFile=$1
sourcePDFFile=$2
pdfOffset=0

while read line; do
   startTag=`echo $line | grep -c "<DESPATCH>"`
   if [[ $startTag -eq 1 ]]; then
      lineCount=0
   fi

   docTag=`echo $line | grep -c "<DESPNO>"`
   if [[ $docTag -eq 1 ]]; then
      docId=`echo $line | cut -f2 -d'>' | cut -f1 -d'<'`
   fi

   lineTag=`echo $line | grep -c "<INVLINE>"`
   if [[ $lineTag -eq 1 ]]; then
      let lineCount+=1
   fi

   endTag=`echo $line | grep -c "</DESPATCH>"`
   if [[ $endTag -eq 1 ]]; then
      pages=`expr \( $lineCount / 12 \) + 1`
      pdfOffset=`expr $pdfOffset + $pages`
      docLast=`expr $pdfOffset + $pages`

      echo "Document: $docId Has: $pages pages for: $lineCount lines"
      /usr/sfw/bin/gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=${pdfOffset} -dLastPage=${docLast} -sOutputFile=${docId}.pdf ${sourcePDFFile}
   fi

done < $dataFile

... I suspect it's the the way I'm invoking expr, I have a funny feeling that's spawning a new command shell to execute.

I tried changing one like to:
pages=$(echo "( $lineCount / 12 ) + 1" | bc)

... but it didnt help much. Any other ideas ?
 
A starting point.
Code:
#!/bin/ksh
dataFile=$1
sourcePDFFile=$2
pdfOffset=0
while read line; do
   case $line in *\<DESPATCH\>*)
      lineCount=0
   esac
   case $line in *\<DESPNO\>*)
      docId=`echo $line | cut -f2 -d'>' | cut -f1 -d'<'`
   esac
   case $line in *\<INVLINE\>*)
      let lineCount+=1
   esac
   case $line in *\</DESPATCH\>*)
      pages=$(((lineCount/12)+1)
      let pdfOffset+=$pages
      docLast=$((pdfOffset + pages))
      echo "Document: $docId Has: $pages pages for: $lineCount lines"
      /usr/sfw/bin/gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=${pdfOffset} -dLastPage=${docLast} -sOutputFile=${docId}.pdf ${sourcePDFFile}
   esac
done < $dataFile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top