The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 12:18 PM
cut from a particular character to the end of the string grajesh_955 Shell Programming and Scripting 2 05-25-2008 03:03 AM
Help needed in character replacement in Korn Shell stevefox Shell Programming and Scripting 8 03-29-2007 08:59 PM
How do I get the nth character from a string? toughman UNIX for Dummies Questions & Answers 4 06-22-2006 09:54 AM
Delete first 2 character from string nitinshinde UNIX for Dummies Questions & Answers 4 10-02-2001 05:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-28-2008
Registered User
 

Join Date: Dec 2007
Posts: 41
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-28-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
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.
Reply With Quote
  #3 (permalink)  
Old 05-28-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Another way :
Code:
MyString=abcde
i=0
while (( i++ < ${#MyString} ))
do
   char=$(expr substr "$MyString" $i 1)
   echo "<$char>"
done
Jean-Pierre.
Reply With Quote
  #4 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: Dec 2007
Posts: 41
Quote:
Originally Posted by aigles View Post
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
Reply With Quote
  #5 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: Dec 2007
Posts: 41
Quote:
Originally Posted by aigles View Post
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
Reply With Quote
  #6 (permalink)  
Old 05-29-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Quote:
Originally Posted by shew01 View Post
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.
Reply With Quote
  #7 (permalink)  
Old 05-29-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Quote:
Originally Posted by shew01 View Post
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0