![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Korn: How to loop through a string character by character
If I have a string defined as:
Code:
MyString=abcde echo $MyString shew01 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
A possible solution :
Code:
MyString=abcde
echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
echo "<$char>"
done
Code:
<a> <b> <c> <d> <e> |
|
#3
|
||||
|
||||
|
Another way :
Code:
MyString=abcde
i=0
while (( i++ < ${#MyString} ))
do
char=$(expr substr "$MyString" $i 1)
echo "<$char>"
done
|
|
#4
|
|||
|
|||
|
Quote:
Code:
awk: syntax error near line 1 awk: bailing out near line 1 shew01 |
|
#5
|
|||
|
|||
|
Quote:
Code:
jps.ksh[3]: i++ < 5 : syntax error |
|
#6
|
||||
|
||||
|
Quote:
Jean-Pierre. |
|
#7
|
||||
|
||||
|
Quote:
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. |
||||
| Google The UNIX and Linux Forums |