RE: Stupid programming question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You could read one file into an shell array then do a loop read of the
second file (like in you example) multiplying against the appropriate
array slot.

You could grab a single number from a file with sed.
Count=$( grep -c '$' file1 )
i=1
while read Num1
do 
  Num2=$( sed -n $i file2 )
  Mul=$(( $Num1 * $Num2 ))
  ...
  i=$(( i + 1 ))
done < file1

You can use $Count to check if you've gone beyond the end of file2.

John Julian


-----Original Message-----
From: linux-admin-owner@xxxxxxxxxxxxxxx
[mailto:linux-admin-owner@xxxxxxxxxxxxxxx] On Behalf Of Michael French
Sent: Wednesday, May 19, 2004 1:06 PM
To: linux-admin@xxxxxxxxxxxxxxx
Subject: Stupid programming question


    I have two text files each with a single column of numbers in them 
and I want to write a script that multiplies the number on each line of 
file one with the corresponding number from file two and produces a sum 
of the multiplied numbers in the 2 files..  How would I do this with a 
loop?  This is just a small shell script, I can use perl if I need to, I

just need to know the data structure.  If it was just one file, I would 
do something like this:

SUM=0
NUMBER1=`cat $FILE1`
for m in $NUMBER1
    do
    SUM=`echo "scale=4; $SUM * $m" |bc`
    done

Not exactly right, but you get the jist of it.  Obviously this won't
work:

SUM=0
NUMBER1=`cat $FILE1`
NUMBER2=`cat $FILE2`
for m in $NUMBER1 and n in $NUMBER2
    do
    SUM=`echo "scale=4; $m * $n" |bc`
    done

Thanks for any help you can provide, let me know if you need more info.

Michael French
-
: send the line "unsubscribe linux-admin"
in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html
-
: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Newbie]     [Audio]     [Hams]     [Kernel Newbies]     [Util Linux NG]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Device Drivers]     [Samba]     [Video 4 Linux]     [Git]     [Fedora Users]

  Powered by Linux