How to remove double quotes in a specific column from a CSV file using Powershell script

"ID","Full Name","Age" "1","Jone Micale","25" 

Here is a sample from a CSV file that I created, and now I want to remove double quotes only from the ID and Age column values.

I tried different ways, but I do not want to create a new file from it. I just want to update the file using PowerShell v1.

+1
source share
1 answer

Export-Csv will always put all fields in double quotes, so you need to remove unwanted quotes. Maybe something like this:

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

Regular Expression Distribution:

  • ^ and $ correspond to the beginning and end of a line, respectively ( Get-Content returns an array with lines from a file).
  • "(.*?)" matches the text between two double quotes and captures the match (without double quotes) in the group.
  • ,(.*?), matches the text between two commas and fixes the match (including double quotes) in the group.
  • $1,$2,$3 replaces the match string separated by commas of the first, second and third groups from the match.
+2
source

Source: https://habr.com/ru/post/987230/


All Articles