Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a variable in a shell script

Status
Not open for further replies.

pd123

Programmer
May 22, 2001
48
GB
Very new to this so bear with me.

I have a variable in a shell script which I would like to split into 2 variables when a ',' is encountered.

eg.
var = "some stuff,more stuff"

Into

var1 = "some stuff"
var2 = more stuff"

Thanks.
 
I've already answered this on the Solaris forum, but ...

VAR1=$(echo $VAR | cut -d, -f1)
VAR2=$(echo $VAR | cut -d, -f2)

You could use awk as well, but this method is as good as any.

Greg.
 
I wasn't sure on the best place to post it.

Thanks anyway.
 
IFS = Internal field separators. Default = " "


var="some stuff,more stuff"
IFS=,
set $var
echo $1
some stuff
echo $2
more stuff

IFS=" "
Gregor.Weertman@mailcity.com
 
Another method with IFS

var="first value,second value"
(
IFS=,
echo $var
) | read var1 var2
echo $var1 # => first value
echo $var2 # => second value Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top