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