If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to speed up this bash script ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-19-07, 17:41
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
How to speed up this bash script ?

Hi all,

I wrote a simple script to compute a difference between two
numbers extracted from a text file. It works fine but
performance is not that great. I wonder how to speed up
this script but it goes beyond my current Bash knowledge.
I know that there is a lot of redundancy on my code but
when I first wrote it speed was not much of a concern...

In short, open one file at a time, recognize and grab two numbers
using some pattern, take the difference between them and output to a file.

I really appreciate any help,

S.

Obs. I know that I could have used Perl, C, etc but I want to
learn how to write a more polished shell script.

-----------------------------------------------------------------------------
#
# Input files are named FILE.X, where X runs from 1 to 5000.
#
first=1
last=5000
loop=$first
#
while [ $loop -le $last ]
do
#
# Grab numbers from file FILE.X using filters
#
list1=(`grep "Pattern1" FILE.$loop | awk '{print $3}'`)
list2=(`grep "Pattern2" FILE.$loop | awk '{print $4}'`)
#
# Execute Math (each list has 15000 elements. Compute the
# difference between the lists and
# save the result to a file)
#
i=0
while [ $i -le 14499 ]
do
a=${list1[$i]}
b=${list2[$i]}
c=$(echo "scale=3; $a - $b" | bc)
echo $c > induce.$loop
i=`expr $i + 1`
done
loop=`expr $loop + 1`
done
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On