Say we have a row in CSV that look like so:
| is out encapsulator, and
, is out delimiter (as you might expect).
But say you have some code that needs to have these empty fields filled in, and instead you want to transform this row into something like this:
(We can deal with the trailing comma later) I have tried to use this sed command over this CSV to get the desired result:
However, the regex patter
,, is not zero width, so as it scans over the line, it moves two along, even though we've only dealt with one field. The result is something like this:
The issue is that when we get this far in the substitution:
We've already 'dealt with' the commas around our
||, and we move onto the next pair after the last comma we regexed, which doesn't involve the last one of
How can I make this substitution using sed?
A more elegant way is to use conditional branching:
$ sed ':a;s/,,/,||,/;ta' <<< '|Foo|,,,,,,,,|Bar|,,,,,' |Foo|,||,||,||,||,||,||,||,|Bar|,||,||,||,||,
If a s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script.