string splitting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string splitting
# 1  
Old 09-01-2007
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  
Old 09-01-2007
You can use expr
Code:
var="abc"
expr substr $var 1 1
expr substr $var 2 1
expr substr $var 3 1

# 3  
Old 09-01-2007
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  
Old 09-01-2007
Quote:
Originally Posted by ravi raj kumar
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting delimited string into rows

Hi, I have a requirement that has 50-60 million records that we need to split a delimited string (Delimeter is newline) into rows. Source Date: SerialID UnidID GENRE 100 A11 AAAchar(10)BBB 200 B11 CCCchar(10)DDD(10)ZZZZ Field 'GENRE' is a string with new line as delimeter and not sure... (5 Replies)
Discussion started by: techmoris
5 Replies

2. Shell Programming and Scripting

Splitting string with awk

Input: Debris Linux is a minimalist, desktop-oriented distribution and live CD based on Ubuntu. It includes the GNOME desktop and a small set of popular desktop applications, such as GNOME Office, Firefox web browser, Pidgin instant messenger, and ufw firewall manager. Debris Linux ships... (5 Replies)
Discussion started by: cola
5 Replies

3. UNIX for Dummies Questions & Answers

Help with splitting a string..

Hello I need help with the following. I have strings like #if defined(__def1__) #if defined(__def1__) || defined(__def2__) #if defined(__def1__) && defined(__def2__) && defined(__def3__). #if defined(__def1__) || defined(__def2__) || defined(__def3__). I need to print what is there in... (4 Replies)
Discussion started by: srk
4 Replies

4. Shell Programming and Scripting

splitting words from a string

Hi, I have a string like this in a file, I want to retrive the words separated by comma's in 3 variables. like How do i get that.plz advice (2 Replies)
Discussion started by: suresh_kb211
2 Replies

5. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

6. Shell Programming and Scripting

Perl - Need help on splitting string

Let's say I have a very long string with no spaces but just words stored in $very_long_string. $very_long_string = "aaaaaaaaaaabbbbbbbbbbbccccccccccccdddddddddddd"; I can do this to split the string into 1 character each and store them in an array: @myArray = split(//, $very_long_string); ... (3 Replies)
Discussion started by: teiji
3 Replies

7. Programming

Is this string splitting function OK?

Hello, I am recently working on an application that sends large strings accross a network very often. These then need to be broken up first with '!' and then with ','. My current function (below) works fine for this when not too much data is being sent across the network but segfaults when a... (4 Replies)
Discussion started by: kpedersen
4 Replies

8. Shell Programming and Scripting

Splitting a string with awk

Hi all, I want to split a string in awk and treat each component seperatley. I know i can use: split ("hi all", a, " ") to put each delimited component into array a. However when i want to do this with just a string of chars it does not work split ("hi", a, ""); print a; prints... (6 Replies)
Discussion started by: pxy2d1
6 Replies

9. Shell Programming and Scripting

Splitting a string

Hi, I am new to shell scripting, Can any body suggest me how I can split a string with a delimiter as whitespace into words and store into a array. I have read a line from file, now I want to split the line into words and store in a array for further use. eg : the sky is blue want... (3 Replies)
Discussion started by: smk
3 Replies

10. UNIX for Dummies Questions & Answers

splitting a string in unix

i need to split a line using a delimiter, and store it into a array :( (2 Replies)
Discussion started by: lmadhuri
2 Replies
Login or Register to Ask a Question