Trying to take a string and break each letter into a separate variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to take a string and break each letter into a separate variable
# 1  
Old 10-01-2011
Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of.

(ex: if forum was typed in letter1=f; letter2=o; letter3=r;...)

Thank you

Code:
count=1;

for i in `echo $1 | sed 's/./& /g'`
do
        echo $i
        letter$count=$i
        count=`expr $count + 1`
done


echo $var1

# 2  
Old 10-01-2011
Hi!
I hope this may help,

Code:
for x in $(seq 1 ${#mystr}) do                
eval "echo letter$x=${mystr:$((x-1)):1}"
done

It uses parameter substitution which is very handy when working with strings, and also some goodies like seq and arithmetics.
Best regards,
Lakris
This User Gave Thanks to Lakris For This Post:
# 3  
Old 10-01-2011
The following is kind of what you were going for, though Lakris' example above is why I come to this site, now I can spend Saturday examining the ${#var} (length of var ?)and ${var:$offset:$length} (substring ?) functions.

Code:
#!/bin/bash

for i in $(echo $1 | sed 's/./& /g'); do
        letters[$count]=$i
        count=$(($count+1))
done
for i in $(seq 0 $((${#letters[@]}-1))) ; do
        echo $i letter is ${letters[$i]}
done


Last edited by Skrynesaver; 10-01-2011 at 05:41 AM.. Reason: added length of array notation
# 4  
Old 10-01-2011
Smilie
But all credit should go to where i found it, 10 Steps to Beautiful Shell Scripts .
A wonderful site that can keep You going through sunday as well.

/Lakris
# 5  
Old 10-01-2011
Quote:
Originally Posted by Skrynesaver
The following is kind of what you were going for, though Lakris' example above is why I come to this site, now I can spend Saturday examining the ${#var} (length of var ?)and ${var:$offset:$length} (substring ?) functions.

Code:
#!/bin/bash

for i in $(echo $1 | sed 's/./& /g'); do
        letters[$count]=$i
        count=$(($count+1))
done
for i in $(seq 0 $((${#letters[@]}-1))) ; do
        echo $i letter is ${letters[$i]}
done


When I try to do this, it doesn't save anything into the variables. it leaves them all blank
# 6  
Old 10-01-2011
Quote:
Originally Posted by crimputt
When I try to do this, it doesn't save anything into the variables. it leaves them all blank
Looks good to me. What does your output look like?

Code:
$
$
$ cat -n var.sh
     1  #!/bin/bash
     2
     3  for i in $(echo $1 | sed 's/./& /g'); do
     4          letters[$count]=$i
     5          count=$(($count+1))
     6  done
     7  for i in $(seq 0 $((${#letters[@]}-1))) ; do
     8          echo $i letter is ${letters[$i]}
     9  done
    10
$
$
$ ./var.sh forum
0 letter is f
1 letter is o
2 letter is r
3 letter is u
4 letter is m
$
$
$ echo $SHELL
/bin/bash
$
$

tyler_durden
# 7  
Old 10-01-2011
I am trying to echo out

echo letter1
echo letter2
echo letter3

and it returns nothing


I am unsure about what this does:

for i in $(seq 0 $((${#letters[@]}-1))) ; do
echo $i letter is ${letters[$i]}
done


but it appears that it does not save the letters into seperate variables? Sorry for the confusion
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk separate field by letter and ";"

i have a 2 fields in my DB ID25333,1429291340lNormPUC-AP_MEX_UFM-GOL_44;PUC-AP_VEX_UFM-ROL_55;PUCAP_MEX_UFM-DOJ_49; ID55555,1429291340lNormPUC-AP_PPP_UFM-HOL_44;PUC-AF_GEX_UJM-SOL_45;PUCAP_MEX_UFM-DOJ_59;and i need separate like this ID25333,PUC-AP_MEX_UFM-GOL_44; ... (5 Replies)
Discussion started by: Axl_north
5 Replies

2. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

3. Shell Programming and Scripting

Setting letter as variable

I need to pass letter like "c" for "-copy" and need to read by variable $sel_mod, My I know the good way of writing the script. Example: Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy') Type the Clone mode : c Thanks in advance, Ashan ... (6 Replies)
Discussion started by: ashanabey
6 Replies

4. Shell Programming and Scripting

change each letter of a string

Is there a way to change each letter of a string to the next one in the alphabet, so that a becomes b and f becomes g, and digits become one unit bigger - 4 becomes 5 and 9 becomes 0. I want to change strings like ben123 to cfo234. (5 Replies)
Discussion started by: locoroco
5 Replies

5. Shell Programming and Scripting

Trying to capitalize first letter of every word in Variable

Total Bash noob, have been successful in doing my script by searching and looking at examples, but I need some assitance with this one, just can't figure it out. In the Bash script I am trying to capitalize the first letter of every word in a string, ideally not changing other capitalization. ... (5 Replies)
Discussion started by: randyharris
5 Replies

6. Shell Programming and Scripting

First Letter in a String to be capitalized

Is there a way to cnvert first letter alone in a string to upper case. For eg: diamond should be converted to Diamond. Thanks in Advance, Kinny (6 Replies)
Discussion started by: kinny
6 Replies

7. Shell Programming and Scripting

Extract the letter from string

Hi, Pl help me. User is entering the values for the given input Enter keys: secret1 secret2 secret3 can i extract the last digit of each values in the keys.. here for ex. 1 2 3 can we get this using shell script? (22 Replies)
Discussion started by: balamv
22 Replies

8. Shell Programming and Scripting

Finding a letter in a string

How to check whether a particular string contains dot or not? Here I can not use grep as the string is not in a file. I will get this string from user input Thanks, (2 Replies)
Discussion started by: balamv
2 Replies

9. Shell Programming and Scripting

Break a file into separate files

Hello I am facing a scenario where I have a file with XML content and I am running shell script over it. But the problem is the XML is getting updated with new services. In the below scenario, my script takes values from the xml file from one service name say ABCD. Since there are multiple, it is... (8 Replies)
Discussion started by: chiru_h
8 Replies

10. Shell Programming and Scripting

get only two letter from any string

I want get middle two latter of any string. Input: var="070108" output: var1="01" please help. (2 Replies)
Discussion started by: rinku
2 Replies
Login or Register to Ask a Question