Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

split string using separetor

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 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 06:06 AM.. Reason: Incorrect
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 06-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 16 July 2015, 8:07 AM EDT
Location: classification algos
Posts: 3,206
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 Unix and Linux 06-06-2007
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 16 March 2015, 4:56 AM EDT
Location: Bangalore,India
Posts: 2,136
Thanks: 14
Thanked 151 Times in 144 Posts

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

The Following 2 Users Say Thank You to anbu23 For This Useful Post:
AntumDeluge (01-23-2015), thinkinmachine (03-18-2011)
Sponsored Links
    #6  
Old Unix and Linux 06-06-2007
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 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 Unix and Linux 06-06-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:12 AM
split the string asth Shell Programming and Scripting 5 09-18-2008 10:46 AM
Split A String newbie187 Shell Programming and Scripting 2 06-28-2008 03:30 PM
split string help senthilk615 Shell Programming and Scripting 4 03-27-2006 05:43 PM
split a string gazingdown Shell Programming and Scripting 3 02-09-2006 04:34 AM



All times are GMT -4. The time now is 11:23 AM.