Computing.Net > Forums > Unix > Parse integer variable and count ea

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.

Parse integer variable and count ea

Reply to Message Icon

Name: thoppz
Date: December 2, 2004 at 15:01:01 Pacific
OS: HP:UX
CPU/Ram: 1
Comment:

I've a variable inum= 14232232345
I need to parse the variable so I can count
1+4+2+3+2.........= 45

thanks,




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 2, 2004 at 21:08:52 Pacific
Reply:

Hi:

I think you're asking to parse and add each character. If so, isn't the string above actually equal 31 and not 45?

Here's a ksh method:

#!/bin/ksh

i=0
d="14232232345"

while [ ! -z $d ]
do
x=$(echo "$d"|cut -c1 )
((i=$i+$x))
d=$(echo ${d##?} ) # keep removing first char
done
echo $i


0

Response Number 2
Name: Jim Boothe
Date: December 3, 2004 at 08:49:51 Pacific
Reply:

And here's a one-line solution:

expr $(echo 14232232345|sed 's/\(.\)/\1 + /g')0


0

Response Number 3
Name: nails
Date: December 3, 2004 at 09:48:20 Pacific
Reply:

Jim:

That's an excellent solution - especially if I understood what you were doing.

Would you mind explaining it, please.


Nails


0

Response Number 4
Name: Jim Boothe
Date: December 3, 2004 at 11:46:22 Pacific
Reply:

Sorry 'bout that ...

I use sed to change each character to that character followed by space and +, which gives you

1 + 4 + 2 + ...

because expr requires all operands to be space delimited.


0

Response Number 5
Name: nails
Date: December 4, 2004 at 16:34:08 Pacific
Reply:

Jim:

I get it: the \( and \) saves any match - a single character in this case, and then uses the back reference, \1, to print the match plus adding a plus sign:

sed 's/\(.\)/\1 + /g'

Then you add a "0" to properly terminate an arithmetic statment before using the result as a parameter to the expr command. Very COOL!

With ksh, you can make a slight mod and assign the result to a variable using eval:

eval "((i=$(echo 14232232345|sed 's/\(.\)/\1 + /g')0))"

Thanks for the lession.

Nails


0

Related Posts

See More



Response Number 6
Name: WilliamRobertson
Date: December 4, 2004 at 17:06:42 Pacific
Reply:

Perhaps the sed expression can be simplified slightly, since we are only replacing one pattern:

expr $(echo 14232232345|sed 's/./& + /g')0

or using ksh arithmetic:

echo $(( $(echo 14232232345|sed 's/./& + /g')0 ))

or just for fun,

let n=14232232345 total=0

while (( n > 0 ))
do
    (( total += n%10 ))
    (( n/=10 ))
done

print $total


0

Response Number 7
Name: Jim Boothe
Date: December 6, 2004 at 07:12:22 Pacific
Reply:

Thanks the good feedback guys. I think it has finally registered with me that a single sed expression does not need numbering!


0

Response Number 8
Name: Jim Boothe
Date: December 6, 2004 at 07:13:17 Pacific
Reply:

... does not need numbering OR isolating.


0

Sponsored Link
Ads by Google
Reply to Message Icon

how to print in BOLD/COLO... Display file date as a 3 ...



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: Parse integer variable and count ea

parsing a variable to a function www.computing.net/answers/unix/parsing-a-variable-to-a-function-/5613.html

Squaring Shell Script Problem www.computing.net/answers/unix/squaring-shell-script-problem/5092.html

Persistant error www.computing.net/answers/unix/persistant-error/4947.html