LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-21-2010, 02:28 PM   #1
suryaemlinux
Member
 
Registered: Mar 2009
Distribution: CentOS, Fedora
Posts: 34

Rep: Reputation: 0
Reading comma separated variable into other variables in shell script


Hi,
In shell script, I have a variable var = xyz, inn, day, night, calif ....n and I would like to read them in to var1 = xzy, var2 = inn, var3= day, var4 = night....var[n].
probably in a loop. I would like to read the variables until end of the line. Comma is the delimiter and there's no comma at the end.

For example:

var = inn, day, grocery

I would like to read it like

var1 = inn, var2 = day, var3 = grocery

Another case:
var = day, grocery, store, road, highway

I would like to read it as

var1 = day, var2 = grocery, var3 = store, var4 = road, var5 = highway
 
Old 12-21-2010, 04:16 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Code:
list=(`echo $var | tr ',' ' '`)
should work, I think. It uses 'tr' to replace the commans with spaces and builds that into a list
 
Old 12-21-2010, 11:45 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,520

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
Or you can use simple substitution:
Code:
arr=( ${var//,/ } )
 
Old 12-22-2010, 09:58 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
It should be noted that the solutions that have been posted are probably the closest solution to the original question that can be reached. The OP asked for a way to magically create a series of scalar variables with ascending numeric suffixes. This is probably impossible, however the solutions given by Snark1994 and grail create a single array variable, which should be a close enough (probably superior) approximation.
Not given in either of the solutions was a method to access individual elements of the array variable (and which some of us find non-obvious) :
Code:
# re: grail's method

echo ${arr[1]}
--- rod.
 
Old 12-22-2010, 12:37 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Code:
#!/bin/bash
var="inn, day, grocery"
IFS=" ,"
i=0
for val in ${var}
do
  i=$((++i))
  eval var${i}="${val}"
done
for ((j=1;j<=i;++j))
do
  name="var${j}"
  echo ${name}=${!name}
done
Output:
Code:
$ ./suryaemlinux
var1=inn
var2=day
var3=grocery
 
Old 12-22-2010, 07:38 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,520

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
Thanx rod ... sometimes forget what seems like the obvious part
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Randomize the comma separated string in shell mariakumar Linux - General 13 10-11-2010 12:32 AM
script to store comma seperated vales in different variables akhtar.bhat Programming 2 11-02-2009 01:02 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM


All times are GMT -5. The time now is 05:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration