Remove comma when between quotes from batch or PoSh

折月煮酒 提交于 2021-02-05 07:33:11

问题


I have a CSV file with a content like:

A,B,C
D,"E,F",G
H,I,"J,K,L"

I need to remove the commas when between quotes (also remove the quotes, but that is not so important):

A,B,C
D,EF,G
H,I,JKL

I looked at PoSh -replace operator but I can't get it to capture multiple group values:

 PS >"D,`"E,F`",G" -replace "`"((?:[^,`"]+)\,?)+`"", '$1'
 D,F,G

as you can see when the group is repeated, only the last value captured is preserved. Is there a way to do the transformation I want?

https://regex101.com/r/ON1rgp/1/


回答1:


You may define a callback to pass to the Regex::Replace method where you may just grab the part between quotes and remove all , there:

$callback = {  param($match) $match.Groups[1].Value.Replace(',','') }
$s =  "D,`"E,F`",G"
$rex = [regex]'"([^"]*)"'
$rex.Replace($s, $callback)

The regex is "([^"]*)" that matches ", then captures into Group 1 (i.e. the $match.Groups[1].Value) any zero or more chars other than " and then matches ". It will need enhancing in case you have escaped quotes, but the approach will be the same.




回答2:


Here's an alternative to using regex:

Import-Csv your.csv | Foreach-Object { 
    $_.PSObject.Properties | ForEach-Object { $_.Value = $_.Value -replace ',' }
    $_
 } | Export-csv your-new.csv -NoTypeInformation

We use Import-CSV to import the data as a PowerShell object. We then iterate through the value of each property of each row of the CSV to replace the ',' character with nothing. Then we export it to a new CSV.



来源:https://stackoverflow.com/questions/45859551/remove-comma-when-between-quotes-from-batch-or-posh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!