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 to assign each row to a variable, variable1 and variable2 respectively, and then pass it into a custom XML script that I am generating.

Any thoughts on the best approach? perl sed awk?

perl, sed, awk, even shell can do it. So the question is, what language do you know.

Shell code:

while IFS=", " read variable1 variable2 # Read lines splitting on spaces and commas
do
        echo "variable1=${variable1} variable2=${variable2}"
done < inputfile

Thanks much for your response. This appears to be almost working.

Here is what I am working with

while IFS=", " read field1 field2 
do
		echo "${field2}" >> allINeed.txt
done < input.txt

Some of the output that I am getting is like this:

TEST-TEST1234 TESTED TEST, TEST1234

When I run the code you suggested, I am getting this output:

TESTED TEST, TEST1234

What is the best approach to use IFS, given the spaces in the first field.

You would be better to direct the output to the file with the done like this:-

while IFS="," read whatever
do
   something
done < input_file > output_file

You can append to the file rather than overwrite it if you need to, of course.

I don't see how you get the new output from what you had at the beginning. Perhaps you could show us some meaningful input too along with how you are getting it/calling it.

Are you getting a single, comma separated line from your database query? If there are no other messages, you could avoid the temporary file and read from the process.

For ksh try:-

db-query | IFS="," read var1 var2

For bash maybe try:-

IFS="," read var1 var2 < <(db-query)

They both depend on cleaning the output to be just a single line. For sqlplus you can add various things, such as the -S flag and ensure you have set heading off , set feedback off and possibly other things to turn off. Other databases have similar concepts to leave you just the raw output.

I hope that this helps,
Robin

Hi robin, thank you for the output. It is a db2 query that does an export/select against a few tables and returns a comma separated text file. Here are some of the examples.. no sql plus here :slight_smile:

testoutput1, testgroup1
test-output2, testgroup2
test-output3 thisisatest, testgroup3
test output4-test, testgroup4

The real and meaningful output that I need is everything after the comma. I need that written to a text file.

How about

cut -d, -f2- file
 testgroup1
 testgroup2
 testgroup3
 testgroup4

Will def. give this a try!

Corona688 posted an elegant solution for your original specification, that gets rid of excess spaces but it would not work right for the second sample.

In addition to what rbatte posted, another way to get rid of the leading space after the comma would be:

while IFS=, read field1 field2 
do
  echo "${field2# }" 
done < input.txt > allINeed.txt

--
Note: this only works if there are exactly two fields, if there can be more you would need to add a third variable to the read command..

while IFS=, read field1 field2 field3
do
  echo "${field2# }" 
done < input.txt > allINeed.txt

--
Another note, to get consistent results independent of input, the safest approach would be to use printf , rather than echo

printf "%s\n" "${field2# }" 

Thank you all so much for your help. I reviewed a few of these, and I think that the most efficient way of doing this is using:

cut

If you want to catch col#2 and trimm the leading spaces

awk -F', *' '{print $2}' input.txt