The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:18 PM
cut from a particular character to the end of the string grajesh_955 Shell Programming and Scripting 2 05-25-2008 06:03 AM
Help needed in character replacement in Korn Shell stevefox Shell Programming and Scripting 8 03-29-2007 11:59 PM
How do I get the nth character from a string? toughman UNIX for Dummies Questions & Answers 4 06-22-2006 12:54 PM
Delete first 2 character from string nitinshinde UNIX for Dummies Questions & Answers 4 10-02-2001 08:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-28-2008
shew01 shew01 is offline
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
  #2 (permalink)  
Old 05-28-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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 (permalink)  
Old 05-28-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Another way :
Code:
MyString=abcde
i=0
while (( i++ < ${#MyString} ))
do
   char=$(expr substr "$MyString" $i 1)
   echo "<$char>"
done
Jean-Pierre.
  #4 (permalink)  
Old 05-29-2008
shew01 shew01 is offline
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
  #5 (permalink)  
Old 05-29-2008
shew01 shew01 is offline
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
  #6 (permalink)  
Old 05-29-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
  #7 (permalink)  
Old 05-29-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
Sponsored Links
Closed Thread

Bookmarks

Tags
shell script, shell scripting, solaris, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0