linux-admin.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Stupid programming question
@ 2004-05-19 17:06 Michael French
  2004-05-19 22:03 ` Russell Evans
  0 siblings, 1 reply; 4+ messages in thread
From: Michael French @ 2004-05-19 17:06 UTC (permalink / raw)
  To: linux-admin

    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

^ permalink raw reply	[flat|nested] 4+ messages in thread
* RE: Stupid programming question
@ 2004-05-19 17:22 JULIAN, JOHN C (AIT)
  2004-05-19 16:45 ` A. R. Vener
  0 siblings, 1 reply; 4+ messages in thread
From: JULIAN, JOHN C (AIT) @ 2004-05-19 17:22 UTC (permalink / raw)
  To: mfrench, linux-admin

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@vger.kernel.org
[mailto:linux-admin-owner@vger.kernel.org] On Behalf Of Michael French
Sent: Wednesday, May 19, 2004 1:06 PM
To: linux-admin@vger.kernel.org
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
-
To unsubscribe from this list: send the line "unsubscribe linux-admin"
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-05-19 22:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-19 17:06 Stupid programming question Michael French
2004-05-19 22:03 ` Russell Evans
  -- strict thread matches above, loose matches on Subject: below --
2004-05-19 17:22 JULIAN, JOHN C (AIT)
2004-05-19 16:45 ` A. R. Vener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).