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
file splitting UBD Shell Programming and Scripting 7 01-17-2008 03:24 PM
Splitting a string smk Shell Programming and Scripting 3 11-02-2007 02:10 PM
splitting a string in unix lmadhuri UNIX for Dummies Questions & Answers 2 02-08-2007 05:59 AM
Line Splitting evoGage UNIX for Dummies Questions & Answers 7 11-30-2005 09:59 PM
ksh splitting string - html forms frustrated1 Shell Programming and Scripting 5 09-22-2004 08:07 AM

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 09-01-2007
ravi raj kumar ravi raj kumar is offline
Registered User
  
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
string splitting

Hi,
I have a string like 'xyz' and i want to to split the above string into letters for comparision purpose.
any idea pls.
cheers
RRK.
  #2 (permalink)  
Old 09-01-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
You can use expr
Code:
var="abc"
expr substr $var 1 1
expr substr $var 2 1
expr substr $var 3 1
  #3 (permalink)  
Old 09-01-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

You can use the special parameter expansion syntax:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate sub-string extraction of parameter expansion.

set -o nounset
echo

## Define function to list versions.

version()
{
  for command
  do
    if command -v $command >/dev/null 2>&1
    then
      command -p $command --version | head -1
    else
      echo " (Warning -- command \"$command\" not found in PATH.)" >&2
    fi
  done
  return 0
}

## List the version of the commands in this demonstration.

version bash head

echo

s="xyz"
l=${#s}
echo " There are $l characters in |$s|"
echo

for ((i=0;i<l;i++))
do
        echo " Character $i of the string is |${s:$i:1}|"
done

exit 0
Producing:
Code:
% ./s1

GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
head (coreutils) 5.2.1

 There are 3 characters in |xyz|

 Character 0 of the string is |x|
 Character 1 of the string is |y|
 Character 2 of the string is |z|
See man bash for details ... cheers, drl
  #4 (permalink)  
Old 09-01-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ravi raj kumar View Post
Hi,
I have a string like 'xyz' and i want to to split the above string into letters for comparision purpose.

Code:
string=xyz
while [ -n "$string" ]
do
    temp=${string#?}
    letter=${string%"$temp"}
    do_something_with "$letter"
    string=$temp
done
Closed Thread

Bookmarks

Tags
linux commands

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 04:31 AM.


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