Korn: How to loop through a string character by character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn: How to loop through a string character by character
# 1  
Old 05-28-2008
Korn: How to loop through a string character by character

If I have a string defined as:

Code:
MyString=abcde

echo $MyString

How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it.

shew01
# 2  
Old 05-28-2008
A possible solution :
Code:
MyString=abcde
echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
   echo "<$char>"
done

Output:
Code:
<a>
<b>
<c>
<d>
<e>

Jean-Pierre.
# 3  
Old 05-28-2008
Another way :
Code:
MyString=abcde
i=0
while (( i++ < ${#MyString} ))
do
   char=$(expr substr "$MyString" $i 1)
   echo "<$char>"
done

Jean-Pierre.
# 4  
Old 05-29-2008
Quote:
Originally Posted by aigles
A possible solution :
Code:
MyString=abcde
echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
   echo "<$char>"
done

Output:
Code:
<a>
<b>
<c>
<d>
<e>

Jean-Pierre.
I am running the Korn shell on Solaris 8, and I am getting errors:

Code:
awk: syntax error near line 1
awk: bailing out near line 1

Any ideas?

shew01
# 5  
Old 05-29-2008
Quote:
Originally Posted by aigles
Another way :
Code:
MyString=abcde
i=0
while (( i++ < ${#MyString} ))
do
   char=$(expr substr "$MyString" $i 1)
   echo "<$char>"
done

Jean-Pierre.
My script name is jps.ksh. Any ideas?

Code:
jps.ksh[3]:  i++ < 5 : syntax error

# 6  
Old 05-29-2008
Quote:
Originally Posted by shew01
I am running the Korn shell on Solaris 8, and I am getting errors:

Code:
awk: syntax error near line 1
awk: bailing out near line 1

Any ideas?

shew01
Do you Have tried nawk instead of awk ?

Jean-Pierre.
# 7  
Old 05-29-2008
Quote:
Originally Posted by shew01
My script name is jps.ksh. Any ideas?

Code:
jps.ksh[3]:  i++ < 5 : syntax error

Sorry, this syntax works with bash but not with ksh.
Try this new version of the script :
Code:
MyString=abcde
i=1
while (( i <= ${#MyString} ))
do
   char=$(expr substr "$MyString" $i 1)
   echo "<$char>"
   (( i += 1 ))
done


Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

4. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

5. Shell Programming and Scripting

delete new line character ( - ) , korn shell

Hi guys , i need help so bad on this issue.. Basically i have to delete the line continuation symbol of first column variable and add the truncated part of that word in next line to first line. here i written sample 3 lines but originally i have bunch of lines in that file. client1_day- ... (3 Replies)
Discussion started by: chrismorgan
3 Replies

6. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

7. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

8. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

9. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

10. Shell Programming and Scripting

Help needed in character replacement in Korn Shell

How do I replace a space " " character at a particular position in a line? e.g. I have a file below $ cat i2 111 002 A a 33 0011 B c 2222 003 C a I want all the 1st spaces to be replaced with forward slash "/" and the 3rd spaces to have 5 spaces to get the output below: 111/002... (8 Replies)
Discussion started by: stevefox
8 Replies
Login or Register to Ask a Question