0
votes

I have exported a CSV file from Power Shell with 3 columns. The first column I need to have double quotes but I do not want quotes in column 2 and 3. Is there a way to specify this when I export the csv file or is there some way I can modify the format to the csv file to remove the double quotes from column 2 and 3?

Current Format

"Column1","Column2","Column3",

Format Needed

"Column1",Column2,Column3,

1
Why do you need it like that? Both are valid CSV, one is fully qualified, the other is minimized (but not completely). Any standard CSV parser should be able to read both just fine.Neolisk

1 Answers

0
votes

This script should work for you:

############ Update these ############
$InputFile = 'C:\test\myfile.csv';
$OutputFile = 'c:\test\myfile.new.csv';
######################################

$Lines = Get-Content -Path $InputFile;

$NewLines = @()
foreach ($Line in $Lines) {
    $Match = [Regex]::Match($Line, '(.*?".*?")(.*)');
    $NewLines += $Match.Groups[1].Value + ($Match.Groups[2].Value -replace '"', '');
}

Set-Content -Path $OutputFile -Value $NewLines;

I tested with the following CSV file contents:

Col1,Col2,Col3
"asdf asdf","asdf","asdf"