从PowerShell中的CSV中只删除引号中的逗号

我的最终目标是使用PowerShell将某些CSV文件中的数据转换/导出到具有多个工作表的Excel工作簿中。 我有约90%的工作,但我似乎无法删除CSV文件的内容中的一些逗号。

我尝试了一些正则expression式,但他们没有工作。

到目前为止,我的运气是它不会删除任何内容,也不会删除CSV中的每一个逗号,然后将所有内容放在一个列中,或者删除单元格中包含逗号的所有内容。

以下是我尝试使用的一些CSV内容的示例。

Software Name Vendor Software A Vendor A Software B Vendor B Software C Vendor, C Software D Vendor D Software E Vendor, E 

以下是我正在编写的脚本中的代码片段。

 $Excel = new-object -comobject Excel.Application $Excel.SheetsInNewWorkbook = $GetCSV.Count $AddWorkBook = $Excel.Workbooks.Add() $NewWorkSheet=1 foreach ($CSV in $GetCSV) { (Get-Content $CSV | Select-Object -Skip 1) | Set-Content $CSV $Row=1 $Column=1 $WorkSheet = $AddWorkBook.WorkSheets.Item($NewWorkSheet) $Name = $CSV.Name -replace ('.CSV','') $WorkSheet.Name = $Name $GetFile = (Get-Content $CSV) foreach($Line in $GetFile) { $LineContens=$Line -split ',(?!\s*\w+”)' foreach($Cell in $LineContens) { $WorkSheet.Cells.Item($Row,$Column) = $Cell $Column++ } $Column=1 $Row++ } $NewWorkSheet++ } $AddWorkBook.SaveAs($WorkBookName) 

描述

 ,(?!(?<=",)") 

用。。。来代替: 没有

正则表达式可视化

这个正则expression式将执行以下操作:

  • find所有不分隔引号分隔值string的逗号
  • 假定所有值都包含在引号内

现场演示

https://regex101.com/r/uY6iG1/2

示例文本

 "Software Name","Vendor" "Software A","Vendor A" "Software B","Vendor B" "Software C","Vendor, C" "Software D,","Vendor D" "Soft,ware E","Vendor, E" 

更换后

 "Software Name","Vendor" "Software A","Vendor A" "Software B","Vendor B" "Software C","Vendor C" "Software D","Vendor D" "Software E","Vendor E" 

说明

 NODE EXPLANATION ---------------------------------------------------------------------- , ',' ---------------------------------------------------------------------- (?! look ahead to see if there is not: ---------------------------------------------------------------------- (?<= look behind to see if there is: ---------------------------------------------------------------------- ", '",' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of look-ahead ----------------------------------------------------------------------