split string using separetor | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

split string using separetor

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-06-2007
rinku rinku is offline
Registered User
 
Join Date: May 2007
Last Activity: 4 September 2008, 2:38 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 06-06-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts

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 07:06 AM.. Reason: Incorrect
Sponsored Links
    #3  
Old 06-06-2007
rinku rinku is offline
Registered User
 
Join Date: May 2007
Last Activity: 4 September 2008, 2:38 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
thanx.....

it works...
    #4  
Old 06-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

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";

Sponsored Links
    #5  
Old 06-06-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 21 November 2014, 4:45 AM EST
Location: Bangalore,India
Posts: 2,091
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
str1=${string%%;*}
str3=${string##*;}
temp=${string#$str1;}
str2=${temp#;$str3}

The Following User Says Thank You to anbu23 For This Useful Post:
thinkinmachine (03-18-2011)
Sponsored Links
    #6  
Old 06-06-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
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.
Sponsored Links
    #7  
Old 06-06-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts

Code:
string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
var=$(echo $string | awk -F";" '{print $1,$2,$3}')   
set -- $var
echo $1
echo $2
echo $3

output:

Code:
# ./test.sh
abc@hotmail.com
xyz@gmail.com
uvw@yahoo.com

The Following User Says Thank You to ghostdog74 For This Useful Post:
6merderphaze (09-08-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
split string jionnet Shell Programming and Scripting 5 08-16-2011 04:12 AM
split the string asth Shell Programming and Scripting 5 09-18-2008 11:46 AM
Split A String newbie187 Shell Programming and Scripting 2 06-28-2008 04:30 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



All times are GMT -4. The time now is 02:33 PM.