rinku
1
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.
echo $string |cut -d';' -f1 | read str1
echo $string |cut -d';' -f2 | read str2
echo $string |cut -d';' -f3 | read str3
$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";
string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
str1=${string%%;*}
str3=${string##*;}
temp=${string#$str1;}
str2=${temp#;$str3}
1 Like
matrixmadhan's solution is for perl script.
You can do the same thing with ksh :
#!/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:
strings count = 3
first : abc@hotmail.com
second: xyz@gmail.com
third : uvw@yahoo.com
Jean-Pierre.
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:
# ./test.sh
abc@hotmail.com
xyz@gmail.com
uvw@yahoo.com
According to my experience, based on temp variable previous message str2 must be:
str2=${temp%%;*}
Have a nice working day
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
Hi,
What if you do not know how many sub-string will produce after split?
I want the last sub-string.
Actually I want to parse the tree structure of file system.
Input :-
/home/hello/temp1/temp2/temp3
Output :-
temp3
There might be less height of tree.
Could you guys please help me out to do this?
Thanks,
Saurabh
#!/bin/ksh
a='/home/hello/temp1/temp2/temp3'
echo ${a##*/}