Question:
I have a .csv in the following file format:
1 2 |
In: "bob","1234 Main St, New York, NY","cool guy" |
I am looking to remove double quotes that don’t have a comma inside:
1 2 |
Out: bob,"1234 Main St, New York, Ny",cool guy |
Is there a way to do this in Powershell?
I have checked:
- 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
Answer:
Adapting the code from “How to remove double quotes on specific column from CSV file using Powershell script”:
1 2 3 4 |
$csv = 'C:\path\to\your.csv' (Get-Content $csv) -replace '(?m)"([^,]*?)"(?=,|$)', '$1' | Set-Content $csv |
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).
See regex demo