Powershell脚本从CSV中删除双引号,除非双引号内存在逗号 [英] Powershell script to remove double quotes from CSV unless comma exists inside double quotes

查看:337
本文介绍了Powershell脚本从CSV中删除双引号,除非双引号内存在逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件格式的.csv:

I have a .csv in the following file format:

In: "bob","1234 Main St, New York, NY","cool guy"

我要删除没有逗号的双引号:

I am looking to remove double quotes that don't have a comma inside:

Out: bob,"1234 Main St, New York, Ny",cool guy

在Powershell中可以做到这一点吗?

Is there a way to do this in Powershell?

我已检查:

  1. 如何使用Powershell脚本从CSV文件中删除特定列上的双引号
  2. https://social.technet.microsoft.com/Forums/windowsserver/zh-CN/f6b610b6-bfb2-4140-9529-e61ad30b8927/how-to-export-csv-without -doublequote?forum = winserverpowershell
  1. How to remove double quotes on specific column from CSV file using Powershell script
  2. http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/02/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell.aspx
  3. https://social.technet.microsoft.com/Forums/windowsserver/en-US/f6b610b6-bfb2-4140-9529-e61ad30b8927/how-to-export-csv-without-doublequote?forum=winserverpowershell

推荐答案

:

Adapting the code from "How to remove double quotes on specific column from CSV file using Powershell script":

$csv = 'C:\path\to\your.csv'
(Get-Content $csv) -replace '(?m)"([^,]*?)"(?=,|$)', '$1' |
    Set-Content $csv

正则表达式(?m)"([^,]*?)"(?=,|$)在逗号或行尾之前匹配任何" + 0 or more non-commas + " (通过正向预见和强制$匹配的多行选项(?m)实现)换行符,而不仅仅是字符串的结尾.

The regex (?m)"([^,]*?)"(?=,|$) is matching any " + 0 or more non-commas + " before a comma or end of line (achieved with a positive look-ahead and a multiline option (?m) that forces $ to match a newline, not just the end of string).

请参见 regex演示

这篇关于Powershell脚本从CSV中删除双引号,除非双引号内存在逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆