Computing.Net > Forums > Unix > sum or multiply data in files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sum or multiply data in files

Reply to Message Icon

Name: Bill
Date: September 12, 2002 at 10:55:20 Pacific
OS: HP 11.0
CPU/Ram: 8GB
Comment:

how can I do computations on values that are stored within a file?? For example if "file1" contains the number 5 and "file2" contains the number 2, how do I write a script that multiplies these two values together and gives me the desired output of 10?? Any help would be greatly appreciated...



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: September 12, 2002 at 12:04:58 Pacific
Reply:

The more specific you can be about your data, the better answers you will get.

Following uses awk to isolate a specific line in both file1 and file2, and pulls one field from each of those lines.

# !/bin/ksh

a=`awk '$1=="A"{print $2;exit}' file1`
b=`awk '$1=="B"{print $3;exit}' file2`

echo "a=$a"
echo "b=$b"
echo "a times b = $((a*b))"

exit 0

You can use several commands other than awk to read the desired line from your files and cut the desired field (cat, grep, cut, sed) depending on your files. Some examples:

a=`cat file1`
a=`head -1 file1`
a=`grep ABC file1 | cut -c22-28`
head -1 file1 | read word1 word2 a rem


0

Response Number 2
Name: bhubb
Date: September 12, 2002 at 12:57:24 Pacific
Reply:

Okay, that's great...now one more thing, it's showing in 8/9 = 0 and 9/8 = 1...It's obviously in integer format...how do I make it display three decimal places...like (0.889 or 1.125)?? Thanks again!!


0

Response Number 3
Name: LANkrypt0
Date: September 12, 2002 at 13:12:07 Pacific
Reply:

I would say you need to use the calc function. Its a little slower but will give you the decimal numbers. You can use calc and cut to isolate what you need
and take out the ~ that calc puts in its answers with decimals

So you could do.
a=8
b=9
total=`calc "$a / $b" | cut -c 1-6 | tr -d "[~]"

And that would give you the answer of: .8888


0

Response Number 4
Name: bhubb
Date: September 12, 2002 at 13:23:50 Pacific
Reply:

I tried this but it doesn't work...It says:

a=8
b=9
c=calc "$a / $b" | cut -c 1-6 | tr -d "[~]"
ksh: calc: not found

Can this command be done in korn shell??


0

Response Number 5
Name: LANkrypt0
Date: September 12, 2002 at 14:30:23 Pacific
Reply:

You need the ` at the beginning and the end of the line

so:
c=`calc "$a / $b" | cut -c 1-6 | tr -d "[~]"`


0

Related Posts

See More



Response Number 6
Name: Bill
Date: September 12, 2002 at 16:26:39 Pacific
Reply:

Still doesn't work...is that a valid korn shell command??


0

Response Number 7
Name: Jerry Lemieux
Date: September 12, 2002 at 16:52:36 Pacific
Reply:

Modify this script to do what you want. I use bc to get floating point results.

#!/bin/ksh

#################
function calc { #
#################

print "Enter a number (can be a decimal): \c"
read FirstNum

print "Enter a number (can be a decimal): \c"
read SecondNum

print "Enter an operator ( + - / * ): \c"
read Operator # Oh can you help me make this call.

case $Operator in

+ | - | / | \* ) continue
;;
* ) print "Invalid operator"; return 99
;;

esac

result=$(print "scale = 2; $FirstNum $Operator $SecondNum" | bc )

print $result

} # end of the calc function.

calc


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sum or multiply data in files

Need help extracting data from file www.computing.net/answers/unix/need-help-extracting-data-from-file/7836.html

flexibility in file naming schemes?? www.computing.net/answers/unix/flexibility-in-file-naming-schemes/570.html

Help with GPG plz www.computing.net/answers/unix/help-with-gpg-plz/5604.html