【问题标题】:Remove only commas in quotations from a CSV in PowerShell从 PowerShell 中的 CSV 仅删除引号中的逗号
【发布时间】:2016-09-26 10:45:45
【问题描述】:

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

我尝试了一些正则表达式,但它们不起作用。

到目前为止,我的运气是它要么不删除任何内容,要么删除 CSV 中的每个逗号,然后通过将所有内容放入单个列来中断导出到 Excel,或者删除单元格中包含逗号的所有内容。

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

Software Name     Vendor

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

这是我正在整理的脚本中的一个 sn-p 代码。

$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)

【问题讨论】:

  • 这不是很清楚。对于 CSV 解析,您不使用 Import-Csv 是否有原因? (technet.microsoft.com/en-us/library/ee176874.aspx)。您尝试在脚本中的哪个位置删除逗号,或者是因为您尝试手动解析 CSV?如果您使用 Import-CSV 并遍历该对象,是否可以解决您的问题?
  • 请学会缩进你的代码。它将使其更具可读性。

标签: regex excel csv powershell


【解决方案1】:

说明

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

替换为: nothing

此正则表达式将执行以下操作:

  • 查找所有不分隔引号分隔值字符串的逗号
  • 假设所有值都包含在引号内

示例

现场演示

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
----------------------------------------------------------------------

【讨论】:

  • 你从哪里得到正则表达式图形图像?
  • 这似乎成功了,完美。非常感谢。
  • 你可以使用负面的lookbehind..它在powershell中得到很好的支持
  • @Robin 该图表是在debuggex.com 上创建的。 @rock321987 是的,您也可以使用否定的lookbehind,但是(?&lt;!"),(?!") 运行两个单独的检查,它们用作“或”语句。这与此答案中包含的“和”语句不同。
猜你喜欢
  • 2022-10-22
  • 2015-07-26
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2023-03-03
相关资源
最近更新 更多