Reading comma separated variable into other variables in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading comma separated variable into other variables in shell script
# 1  
Old 12-21-2010
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
# 2  
Old 12-21-2010
Code:
 
#!/bin/ksh
IFSsave="$IFS"
IFS=,
var="day,grocery,store,road,highway"
set $var
typeset -i i
i=1
while [ i -lt $# ]; do
    eval var$i=\$$(echo $i)
    eval echo "var$i=\$var$i"
    i=i+1
done
IFS="$IFSsav"


Last edited by anurag.singh; 12-21-2010 at 05:06 PM..
# 3  
Old 12-21-2010
Arrays are nice for this sort of stuff (you can loop through them with counters, etc):

In bash:
Code:
#!/bin/bash
var=day,grocery,store,road,highway
IFSsave="$IFS"
IFS=,
vars=( $var )
IFS="$IFSsave"
for((i=0;i<${#vars[@]};i++))
do
   echo var$i=${vars[i]}
done

In ksh:
Code:
#!/bin/ksh
var=day,grocery,store,road,highway
IFSsave="$IFS"
IFS=,
set -A vars $var
IFS="$IFSsave"
i=0
while [ $i -lt ${#vars[@]} ]
do
   echo var$i=${vars[i]}
   let i=i+1
done

# 4  
Old 12-21-2010
Quote:
Originally Posted by Chubler_XL
Arrays are nice for this sort of stuff (you can loop through them with counters, etc):

In bash:
Code:
#!/bin/bash
var=day,grocery,store,road,highway
IFSsave="$IFS"
IFS=,
vars=( $var )
IFS="$IFSsave"
for((i=0;i<${#vars[@]};i++))
do
   echo var$i=${vars[i]}
done

In ksh:
Code:
#!/bin/ksh
var=day,grocery,store,road,highway
IFSsave="$IFS"
IFS=,
set -A vars $var
IFS="$IFSsave"
i=0
while [ $i -lt ${#vars[@]} ]
do
   echo var$i=${vars[i]}
   let i=i+1
done


I'm using bash. I tried your code. but the var1 reads var0=day instead of just day
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Comma Separated values to UNIX variable from PLSQL

Hi All, I'm trying to pass the comma separated values (string) returned from Plsql Procedure to UNIX variable. getting the below log message cat: -: Bad file descriptor awk: cmd. line:1: fatal: error reading input file `-': Bad file descriptor The output coming from plsql procedure is... (3 Replies)
Discussion started by: Mahesh3089
3 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Assign comma separated values to a variable

Hi All, I wrote a database command that queries our application and outputs a whole bunch of values to a text file. I need to assign the output to two values. Here is a sample of the output: valueOne, checkOne valueTwo, checkTwo valueThree, checkThree I would like... (9 Replies)
Discussion started by: jeffs42885
9 Replies

4. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

5. Shell Programming and Scripting

Reading Words separated by comma in line

Hi All, I am facing issue, to read words in line, line as follow and i want to read word at each comma 1,you,are,two So i want read like 1 you are two Thanks (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

6. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

7. Shell Programming and Scripting

reading comma separated data and reorder

hey guys! i need to read data from a file that are comma separated then reorder them in another file to be generated for example: x,y,z a,b,c l,m,n o,p,q and transform this into: x,a,l,o y,b,m,p z,c,n,q Will appreciate your fast reply Regards! (5 Replies)
Discussion started by: maiooi90
5 Replies

8. Shell Programming and Scripting

script to store comma separated values in different variables

Hello friends, I need ur help.I want to write a script. The script should read contents from a file namely xyz. e.g xyz abcd,1234,efgh,7854378 dhnsa,dsakjkdl,43432,ZXDsa the script should store comma (,) seperated values in different variables. Once pointer will reach end of line (\n), it should... (1 Reply)
Discussion started by: akhtar.bhat
1 Replies

9. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies

10. UNIX for Dummies Questions & Answers

shell script, reading and resetting local variables

Hello, I have a problem with trying to run a shell script that reads in user input, validates, and sets to a 'default' value if the input is not valid. I cannot get the portion of resetting to a default value to work. These lines are skipped, and the value of x is still whatever the user... (1 Reply)
Discussion started by: b888c
1 Replies
Login or Register to Ask a Question