加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – Powershell脚本从CSV中删除双引号,除非双引号内存

发布时间:2020-12-13 21:54:05 所属栏目:百科 来源:网络整理
导读:我有以下文件格式的.csv: In: "bob","1234 Main St,New York,NY","cool guy" 我想删除里面没有逗号的双引号: Out: bob,Ny",cool guy 有没有办法在Powershell中做到这一点? 我检查过: How to remove double quotes on specific column from CSV file usin
我有以下文件格式的.csv:
In: "bob","1234 Main St,New York,NY","cool guy"

我想删除里面没有逗号的双引号:

Out: bob,Ny",cool guy

有没有办法在Powershell中做到这一点?

我检查过:

> 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

调整 “How to remove double quotes on specific column from CSV file using Powershell script”的代码:
$csv = 'C:pathtoyour.csv'
(Get-Content $csv) -replace '(?m)"([^,]*?)"(?=,|$)','$1' |
    Set-Content $csv

正则表达式(?m)“([^,] *?)”(?=,| $)匹配逗号或行尾之前的任何“0或更多非逗号”(以正面预测和多线选项(?m)强制$匹配换行符,而不仅仅是字符串的结尾.

见regex demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读