Let's say I have the following sentence
Apples, "This, is, a test",409, James,46,90
I want to change the commas inside the quotation marks by ;
. Or, alternatively, the ones outside the quotation marks by the same character ;
. So far I thought of something like
perl -pe 's/(".*)\K,(?=.*")/;/g' <mystring>
However, this is only matching the last comma inside quotation marks because I am restarting the regex engine with \K
. I also tried some regex's to change the commas outside quotation marks but I can't get it to work.
Note that the spaces after commas outside the quotation marks are there on purpose, so that
perl -pe 's/,\s/;/g' <mystring>
is not a valid answer.
The desired output would be
Apples, "This; is; a test",409, James,46,90
Or alternatively
Apples; "This, is, a test";409; James;46;90
Any thoughts on how to approach this problem?