I'm attempting to create a KSH array out of a string like this: ",,,value1,value2,,"
I have created the array but I only get two elements, one for value1 and one for value2.
I have ended up with something like this but I don't like it:
set -A JUNK
xx=0
for i in $(print ",,,value1,value2,," | nawk '{gsub(/,/," \n",$0);print}')
do
print ".$i."
JUNK[xx]=$i
(( xx += 1 ))
done
for i in "${JUNK[@]}"
do
print ".$i."
done
The results:
. .
. .
. .
.value1 .
.value2 .
. .
. .
If i leave out the space character before the newline in this gsub command:
gsub(/,/," \n",$0);
...I get this output:
.value1 .
.value2 .
I really don't want the spaces at the end of each string and I nead an array of every CSV field (even the empty values).
I have tried several approaches including changing IFS and looping through the fields but this doesn't seem to work at all.
Where do these csv lines come from? I think that I've seen this sort of question before.
And I think that there is an issue that arises when the data contains a comma. What happens then? I seem to remember that the field with a comma is surrounded by quotes, but I don't remember for sure.
I guess that there is (a perhaps null) field before the first comma and after the last comma. Is this correct?
Sorry, our postings must have crossed paths. The csv values are going to be generated from a Java program that I can direct the design of at this stage. The values represent customer parameters to drive some data loads and they are positional. If there were two parameters I would be making simple assignments but since there are several, I decided to use an array.
I can have any number of csv lines to parse during a load session so it's not like picking up a one-time set of control parameters.
As I indicated in my last post, I have apparently ended up in the most logical approach working but I don't know why. I have worked through this for hours and used, what I thought, the exact approach that I documented with no luck. I stumbled on the working lines when I thought that commas could have been the issue and I replaced them with colons. When colons worked, I tried commas again. They worked too!
I don't get it.
BTW, it seems that you are correct with respect to the quotation marks but that is inconsequential. I only care if the parameter was provided or not. I can cope with quotes.