Assign variables to CSV string (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign variables to CSV string (bash)
# 1  
Old 07-14-2014
Assign variables to CSV string (bash)

Hi guys,

New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot:

I currently have a shell that spits out a CSV number string of about 8 numbers as follows:
Code:
1.00,2.00,3.00 ... ,8.00

I need to assign a unique variable to each of these numbers. (I think that's the right parlance.) So if I were to do this line by line, I would have the following:
Code:
V1=1.00
V2=2.00
V3=3.00
etc

With results that, if I were to use for example "echo", it would look like:
Code:
# echo $V1
#1.00

Is there a way in bash to assign a $V(x) variable to the CSV string numbers?

The long story here is I'm trying to get a an open-source payroll program (written in python) to output the various payroll numbers in CSV format, assign each of those numbers a variable, and have those variables piped into a text file which my accounting program can read via its built-in keystroke progamming language. So far I have the python program spitting out the right numbers, I just can't get the variables to be assigned to the CSV string in the correct fashion.

Thanks for all your patience - Hansol

Last edited by hansol; 07-14-2014 at 05:26 PM..
# 2  
Old 07-14-2014
For something like this, it is much easier to use an array rather than trying to evaluate a variable number of variables. This works with both ksh and bash:
Code:
#!/bin/bash
CSVstring="1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00"
IFS=',' V=($CSVstring)
echo "Elements in array V: ${#V[@]}"
for((i = 0; i < ${#V[@]}; i++))
do	printf "v[%d]=%s\n" $i "${V[i]}"
done

and produces the output:
Code:
Elements in array V: 8
v[0]=1.00
v[1]=2.00
v[2]=3.00
v[3]=4.00
v[4]=5.00
v[5]=6.00
v[6]=7.00
v[7]=8.00

# 3  
Old 07-14-2014
Longhand using OSX 10.7.5, default bash terminal.
Code:
#!/bin/bash
# var_X.sh
> /tmp/var.array
ifs_str="$IFS"
IFS=","
n=0
echo '1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00' >> /tmp/var.array
var=`cat < /tmp/var.array`
echo "$var"
echo ""
var_array=($var)
while [ $n -lt ${#var_array[@]} ]
do
	echo "var$n=${var_array[$n]}"
	eval "var$n=\"${var_array[$n]}\""
	n=$((n+1))
done
echo ""
echo "$var0"
echo "$var1"
echo "$var2"
echo "$var3"
echo "$var4"
echo "$var5"
echo "$var6"
echo "$var7"
IFS="$ifs_str"
exit 0

Results:-
Code:
Last login: Mon Jul 14 22:30:15 on ttys000
AMIGA:barrywalker~> ./var_x.sh
1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00

var0=1.00
var1=2.00
var2=3.00
var3=4.00
var4=5.00
var5=6.00
var6=7.00
var7=8.00

1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
AMIGA:barrywalker~> _

Important note:-
eval is a very powerful and dangerous command; be aware!
Code:
AMIGA:barrywalker~> help eval
eval: eval [arg ...]
    Read ARGs as input to the shell and execute the resulting command(s).
AMIGA:barrywalker~> _

EDIT:
15th July 2014, 13:11pm local UK time; now tested on CygWin...

Last edited by wisecracker; 07-15-2014 at 09:12 AM.. Reason: See above...
# 4  
Old 07-15-2014
Don and Wisecracker,

Thank you for the replies. I've since trialed the code you provided, and I certainly get a string that shows as an output:
Code:
V(x) = [some number]

"
The issue though is that the output doesn't seem to assign a variable to the respective items. For instance, when I ran my latest code, I got the following result:
Code:
v0=900.00
v1=36.00
v2=39.11
v3=39.11
v4=17.60
v5=24.64
v6=0.00
v7=169.12

However, if in the same script, I try to run
Code:
echo $v7

I would expect to get the resulting output of "169.12" to appear. Instead I get nothing.

I apologize in advance, as I am quite sure that I am the problem here, and that my lack of knowledge is what's getting in the way. Thank you for your patience so far -Hansol
# 5  
Old 07-15-2014
Wisecracker and I showed you very different ways to do things. We both showed you working code. Show us the code you're using and we may be able to help you.

Just saying that our code doesn't work when we both showed you output demonstrating that our code does work doesn't give us much to be able to help you.
# 6  
Old 07-15-2014
Don,

My apologies for the vague response, as that wasn't my intent. Here is my code as of current:
Code:
#To create CSV string
NVPR=$(~/location/of/shell/to/output/CSV/string)

#To convert CSV to two decimals 
LL=$(printf "%0.2f",$NVPR) 

#To generate array of V(x) numbers from LL 
CSVstring="$LL"
IFS=',' V=($CSVstring)
#echo "Elements in array V: ${#V[@]}"
for((i = 0; i < ${#V[@]}; i++))
do    printf "v%d=%s\n" $i "${V[i]}"
done

If I add for example a simple
Code:
...(above code string)...
done

echo $v7

to see what number the $v7variable generates, unfortunately nothing comes up
So that's what I'm working with currently. Hopefully that sheds a bit more light on the issue.

Last edited by hansol; 07-15-2014 at 02:45 AM..
# 7  
Old 07-15-2014
Did you look at the description of handling arrays on the bash(1) man page on your system? I had hoped that showing how the array elements were printed in the for loop would have helped.

With a shell array like this, elements in the array have subscripts 0 through (n - 1) where n is the number of elements in the array. To get the value of the element of the array with subscript i, use ${array_name[i]}. The number of elements in the array is ${#array_name[@]}. So to print the 8th element of the array, use:
Code:
echo ${V[7]}

not:
Code:
echo $v7

But, you have an earlier problem. The:
Code:
#To convert CSV to two decimals 
LL=$(printf "%0.2f",$NVPR)

in your script does not convert a CSV string into a CSV string with elements between commas converted to floating point values to two decimal places. It will only convert an initial string of digits (possibly including a single decimal point) into a single floating point value rounded to two digits after the decimal point. So, when you get to the command:
Code:
IFS=',' V=($CSVstring)

there is only one floating point value in $CSVstring and there aren't any commas in the expansion of that variable. So, V will be an array containing only one element.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] - Replace blank and string in csv file

Hi all, i have a .csv file with only two columns, like: Login;Status Luca;S Marco; Stefano; Elettra;S Laura; ... I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like: Login;Status Luca;Disabled Marco;Enabled Stefano;Enabled... (10 Replies)
Discussion started by: kamose
10 Replies

2. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

4. Shell Programming and Scripting

How to read a delimited string and assign fields to incremented variables?

How can I read a string delimited on spaces and assign the fields to incremented variables. For example: Given $exts= txt dat mov I want to read in $exts and have "txt" "dat" and "mov" assigned to incremented variables like $ext1, $ext2, etc. I would like to do this in a loop so that I can... (4 Replies)
Discussion started by: runit
4 Replies

5. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

6. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

7. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

8. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

9. Shell Programming and Scripting

Assign script parameters to variables

Hi, I have a bash script that accepts some parameters as input, like: sh script.sh first second third ..... I want to save these parameters as different variables, something like: VAR1=first VAR2=second etc I tried this, but apparently it didn't worked....... (16 Replies)
Discussion started by: giorgos193
16 Replies

10. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question