Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 18: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

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