I am trying to process a file that is comma-delimited. Within that file, one of the fields contains commas. I have no control over the output of the file, but the field containing the commas is conveniently surrounded by curly brackets. I need to find a way in Powershell (v3) to replace the commas that exist ONLY between the curly brackets. I have tried to do a split/replace and RegEx to no avail.
Here is an example of the data:
"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":"","SubstringField1":"Related Text","SubstringField2":"","SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",
What I would like is an output like this:
"[email protected]","Text-exists-here()","","123456789","11/01/2015","{"ProblemSpot":""~"SubstringField1":"Related Text"~"SubstringField2":""~"SubstringFieldn":"MoreRelatedText"}","MoreData","LastField",
I've tried RegEx:
(gc $file) | -replace "\{.+?(,+?).+\}", "|" | set-content $file
And I've tried a rather long, ugly, hacked split/replace/join solution that works in Powershell v4, but not v3. If possible, I'd love a simple/clean solution to do a replace within the substring.
,(?=[^{}]*})- unless there can be other}somewhere that may break the output. A more complex way is to use({|(?!^)\G)([^,]*),(?=[^{}]*})with$1$2~replacement (this makes sure we only replace inside some pair of{...}).(?<={[^{}]*),(?=[^{}]*}).