LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-23-2012, 10:12 AM   #1
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Rep: Reputation: Disabled
shell script to find the sum of numbers associated with each character in a string


i have just started dealing with shell script.... i want to make a script that adds each letter in a name and gives a total...i ve tried with some commands but i feel that i am falling short of commands, can anyone suggest an appropriate command which can help me with this....
thnx in advance...
 
Old 01-23-2012, 10:15 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
http://tldp.org/LDP/abs/html/string-manipulation.html
 
1 members found this post helpful.
Old 01-23-2012, 10:45 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,474

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
Maybe show what you have tried and where you are stuck?
 
Old 01-24-2012, 03:56 AM   #4
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
that string manipulation page was quite useful..but still i didnt get what i want....
Code:
vi
echo "Enter your name"
read n1
i got upto here and just and have been thinking till now....i dont know what to do for getting
aagam=1+1+7+1+13
 
Old 01-24-2012, 04:17 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
The first step is to use a loop and string manipulation to extract each letter in turn: a, a, g etc.

Have you been able to do that?
 
Old 01-24-2012, 07:33 AM   #6
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Hello Aagam

Code:
#!/bin/bash

read -p "Enter your name: "
echo "${#REPLY}"
The shell has this functionality built-in. I don't know if it's the kind of solution you were searching for though...

EDIT: just re-read what you're looking for... Obviously this not the solution

Last edited by kabamaru; 01-24-2012 at 08:15 AM.
 
Old 01-24-2012, 09:46 AM   #7
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
How about this?

It uses a for loop to iterate through the alphabet, if the letter is in the word it counts how many times. Then it calculates the sum and prints it.

Code:
#!/bin/bash

letternum=0
ALPHABET=(Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz)

read -p "Enter your name: " name

for i in ${ALPHABET[@]}; do
	((++letternum))
	if [ "$(echo $name | grep [$i])" ]; then
		occurences=$(echo $name | tr -cd [$i] | wc -c)
		((sum+=letternum*occurences))
	fi
done

echo "$name: sum: $sum"
 
Old 01-24-2012, 09:53 AM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Also easy done in perl

Code:
echo aagam | perl -ne 'chomp;map {$x+=(ord)-96} split //,lc;print "$x\n"'
 
Old 01-24-2012, 10:18 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

similar to kabamaru's solution but with focus on String manipulation:
Code:
#!/bin/bash

j=0
result=0
arr=({a..z})
read name
while (( j < ${#name} ));do
        i=0
        while (( i < ${#arr[@]} ));do
                [[ ${arr[$i]} = ${name:$j:1} ]] && ((result = result + i + 1)) && break
                ((i++))
        done
        ((j++))
done

echo $result
It can be easily adapted to deal with capital letters, too.

Last edited by crts; 01-24-2012 at 10:19 AM.
 
Old 01-24-2012, 10:38 AM   #10
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
i tried all those one by one, i think that they need minute corrections as i get following outputs.....btw "namesum" is the name by which i ve saved the file
@crts ontrying the method described by you i get
Code:
Syntax error: "(" unexpected
@Cedrik ontrying the method described by you i get
Code:
Syntax error: EOF in backquote substitution
@kabamaru ontrying the method described by you i get
Code:
namesum: 3: Bb: not found
Enter your name: aagam
namesum: 13: Bad substitution
@ catkin, no i am not able to do that..
i would be glad to knw all these methods....
and can anyone give the syntax to find the character in the string by specifying its postion...i tried to do dat with match and index commands but i didnt get desired results...
 
Old 01-24-2012, 10:46 AM   #11
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
I don't know, I 've tested it and it works on my mac and my linux box. Maybe copy-paste didn't work correctly?

try this file (uncompress it with gunzip).
 
Old 01-24-2012, 10:55 AM   #12
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by Aagam View Post
@Cedrik ontrying the method described by you i get
Code:
Syntax error: EOF in backquote substitution
There is no backquote in the code I posted above...
 
Old 01-24-2012, 10:57 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,474

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
How about:
Code:
#!/bin/bash

read -p "Enter name: " name

for (( i = 0; i < ${#name}; i++ ))
do
	(( sum += $(printf "%d" "'${name:i:1}") - 96 ))
done

echo $sum
Of course if you want to play with upper and lower case it will take a little rejigging
 
Old 01-24-2012, 08:13 PM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by Aagam View Post
i tried all those one by one, i think that they need minute corrections as i get following outputs.....btw "namesum" is the name by which i ve saved the file
@crts ontrying the method described by you i get
Code:
Syntax error: "(" unexpected
Not sure why you get that error. I do not get an error when I run it and I checked that there is no typo in my posted solution. Did you type or copy/paste it when you tried it?
 
Old 01-24-2012, 11:39 PM   #15
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
@crts, i copy paste-d it..
@grail i tried dat and it gives
Code:
aagam@infinity:~$ sh namesum
Enter name: aagam
namesum: 5: Syntax error: Bad for loop variable
@kabamaru, it still gives the same error......
is there a possibility of terminal being faulty?
and plz tell me the command used to extract a letter from the string by specifying its position...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 07:51 PM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration