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.
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
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.
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