I try to write KSH script for processing a file consisting of name-value pairs, several of them on each line.
Format is:
NAME1 VALUE1,NAME2 VALUE2,NAME3 VALUE3, etc
Suppose I write:
read l IFS=',' set -A nvls $l echo '$nvls[2]'
This will give me second name-value pair, nice and easy. Now, suppose that the task is extended so that values could include commas. They should be escaped, like this:
NAME1 VALUE1,NAME2 VALUE2_1\,VALUE2_2,NAME3 VALUE3, etc
Obviously, my code no longer works, since ‘read’ strips all quoting and second element of array will be just ‘NAME2 VALUE2_1’.
I’m stuck with older ksh that does not have ‘read -A array’. I tried various tricks with ‘read -r’ and ‘eval set -A ….’, to no avail. I can’t use ‘read nvl1 nvl2 nvl3’ to do unescaping and splitting inside read, since I dont know beforehand how many name-value pairs are in each line.
Does anyone have a useful trick up their sleeve for me?
PS I know that I have do this in a nick of time in Perl, Python, even in awk. However, I have to do it in ksh (… or die trying 😉
You can also change the
\,pattern to something else that is known not to appear in any of your strings, and then change it back after you’ve split the input into an array. You can use the ksh builtin pattern-substitution syntax to do this, you don’t need to use sed or awk or anything.