Operating System - HP-UX
1834051 Members
2560 Online
110063 Solutions
New Discussion

Does anyone know this:? (Number processing!)

 
SOLVED
Go to solution
rveri
Super Advisor

Does anyone know this:? (Number processing!)

Hi All ,

Q: Does anyone know , how add 5 numbers using dc ?

I have checked man page but not able to understand much. Nor I got a good guide to do this.

I can use this command to add 2 number , but unable to do more than 2.

# echo " 5 7 + p " | dc
12
#


Thanks in advance.
11 REPLIES 11
RAC_1
Honored Contributor

Re: Does anyone know this:? (Number processing!)

I can.

export p=9
echo "2+5+$p" |bc

All is fine.
There is no substitute to HARDWORK
Patrice Le Guyader
Respected Contributor

Re: Does anyone know this:? (Number processing!)

Hi,

Another way to add three number :
Hpux:/# let 'p=5+7'
Hpux:/# echo $p
Hpux:/# 12
Hpux:/# let 'q=5+7+p'
Hpux:/# echo $q
Hpux:/# 24

Hope this helps
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Hemmetter
Esteemed Contributor
Solution

Re: Does anyone know this:? (Number processing!)

Hello rveri

Did you try:

# echo " 3 4 5 + + p" | dc
12

echo " 3 4 5 * + p" | dc
23


rgds
HGH
Peter Godron
Honored Contributor

Re: Does anyone know this:? (Number processing!)

rveri,
for 5 number is would be:
# echo " 3 4 5 6 7 + + + + p"|dc
25

Why the use of dc?
Yogeeraj_1
Honored Contributor

Re: Does anyone know this:? (Number processing!)

hi,
if you can put it all in a file,

e.g.

12
23
34
45
45
56


you can then just do:
cat datafile.lis | awk '{sum=sum+$1} End {print sum}'

hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Indira Aramandla
Honored Contributor

Re: Does anyone know this:? (Number processing!)

Hi rveri,

To add 5 numbers using dc you could do either way

echo "5 +5 +5 +5 +5 +p"|dc
25

echo "5 5 5 5 5 +++++p"|dc
25



Indira A
Never give up, Keep Trying
Muthukumar_5
Honored Contributor

Re: Does anyone know this:? (Number processing!)

Try as,

# echo "5 7 9 11 13 + + + + p" | dc

More ways are there,

a) expr
b) awk
c) echo $(())
d) perl

etc.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Does anyone know this:? (Number processing!)

Hello,

Easiest way in perl,

# perl -e "print 10+20+30+40+50"

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Raj D.
Honored Contributor

Re: Does anyone know this:? (Number processing!)

hi,

You can use
# echo " 1 2 3 4 +++ p " | dc
10
#


You can also use :
1. awk
2. expr
3. echo
4. perl
5. sed
6. bc


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Arunvijai_4
Honored Contributor

Re: Does anyone know this:? (Number processing!)

Hi,

The Logic with "dc" is simple, number of + signs = (n-1)

# echo "1 2 3 4 5 + + + + p" | dc

You need to specify four + signs here.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arturo Galbiati
Esteemed Contributor

Re: Does anyone know this:? (Number processing!)

Hi Rveri,
why not?
echo "1+2+3+4+5"|bc
HTH,
Art