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
[KSH] Split string into array piooooter Shell Programming and Scripting 3 09-01-2007 12:22 PM
KSH split string into variables drd_2b Shell Programming and Scripting 5 04-23-2006 07:07 PM
split string help senthilk615 Shell Programming and Scripting 4 03-27-2006 06:43 PM
split a string gazingdown Shell Programming and Scripting 3 02-09-2006 05:34 AM
split a file at a specified string jpl35 Shell Programming and Scripting 6 07-04-2002 11:41 AM

Reply
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 06-06-2007
rinku rinku is offline
Registered User
  
 

Join Date: May 2007
Posts: 54
split string using separetor

i have one string , I want to split that string.
exmp:


string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"

I want to split it and save it in three variable
str1=abc@hotmail.com
str2=xyz@gmail.com
str3=uvw@yahoo.com

I want to split using ';'.

please help.
  #2 (permalink)  
Old 06-06-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Code:
echo $string |cut -d';' -f1 | read str1
echo $string |cut -d';' -f2 | read str2
echo $string |cut -d';' -f3 | read str3

Last edited by lorcan; 06-06-2007 at 06:06 AM.. Reason: Incorrect
  #3 (permalink)  
Old 06-06-2007
rinku rinku is offline
Registered User
  
 

Join Date: May 2007
Posts: 54
thanx.....

it works...
  #4 (permalink)  
Old 06-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Code:
$str = "abc\@hotmail.com;xyz\@gmail.com;uvw\@yahoo.com";

@arr = split(/;/, $str);

print "first: $arr[0]\n";
print "second: $arr[1]\n";
print "third: $arr[2]\n";
  #5 (permalink)  
Old 06-06-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
str1=${string%%;*}
str3=${string##*;}
temp=${string#$str1;}
str2=${temp#;$str3}
  #6 (permalink)  
Old 06-06-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
matrixmadhan's solution is for perl script.
You can do the same thing with ksh :
Code:
#!/usr/bin/ksh

string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"

oIFS="$IFS"; IFS=';' 
set -A str $string
IFS="$oIFS"

echo "strings count = ${#str[@]}"
echo "first : ${str[0]}";
echo "second: ${str[1]}";
echo "third : ${str[2]}";
Output:
Code:
strings count = 3
first : abc@hotmail.com
second: xyz@gmail.com
third : uvw@yahoo.com
Jean-Pierre.
  #7 (permalink)  
Old 07-09-2008
s. murat's Avatar
s. murat s. murat is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
Cool a problem occurs with "str2" solution

According to my experience, based on temp variable previous message str2 must be:

str2=${temp%%;*}

Have a nice working day
Reply

Bookmarks

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 05:39 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