我有以下文件格式的 .csv:
In: "bob","1234 Main St, New York, NY","cool guy"
我想删除里面没有逗号的双引号:
Out: bob,"1234 Main St, New York, Ny",cool guy
有没有办法在 Powershell 中做到这一点?
我检查过:
最佳答案
改编来自 "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)
实现,强制 $
匹配换行符,而不仅仅是字符串的结尾)。见 regex demo
关于regex - 除非双引号内存在逗号,否则从 CSV 中删除双引号的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247002/