ConvertTo-Csv Output without quotes

前端 未结 14 1037
慢半拍i
慢半拍i 2020-12-01 08:03

I am using ConvertTo-Csv to get comma separated output

 get-process | convertto-csv -NoTypeInformation -Delimiter \",\"

It out

14条回答
  •  孤独总比滥情好
    2020-12-01 08:44

    I ran into this issue, found this question, but was not satisfied with the answers because they all seem to suffer if the data you are using contains a delimiter, which should remain quoted. Getting rid of the unneeded double quotes is a good thing.

    The solution below appears to solve this issue for a general case, and for all variants that would cause issues.

    I found this answer elsewhere, Removing quotes from CSV created by PowerShell, and have used it to code up an example answer for the SO community.

    Attribution: Credit for the regex, goes 100% to Russ Loski.

    Code in a Function, Remove-DoubleQuotesFromCsv

    function Remove-DoubleQuotesFromCsv
    {
        param (
            [Parameter(Mandatory=$true)]
            [string]
            $InputFile,
    
            [string]
            $OutputFile
        )
    
        if (-not $OutputFile)
        {
            $OutputFile = $InputFile
        }
    
        $inputCsv = Import-Csv $InputFile
    
        $quotedData = $inputCsv | ConvertTo-Csv -NoTypeInformation
    
        $outputCsv = $quotedData | % {$_ -replace  `
            '\G(?^|,)(("(?[^,"]*?)"(?=,|$))|(?".*?(?

    Test Code

    $csvData = @"
    id,string,notes,number
    1,hello world.,classic,123
    2,"a comma, is in here","test data 1",345
    3,",a comma, is in here","test data 2",346
    4,"a comma, is in here,","test data 3",347
    5,"a comma, is in here,","test data 4`r`nwith a newline",347
    6,hello world2.,classic,123
    "@
    
    $data = $csvData | ConvertFrom-Csv
    "`r`n---- data ---"
    $data
    
    $quotedData = $data | ConvertTo-Csv -NoTypeInformation
    "`r`n---- quotedData ---"
    $quotedData
    
    # this regular expression comes from:
    # http://www.sqlmovers.com/removing-quotes-from-csv-created-by-powershell/
    $fixedData = $quotedData | % {$_ -replace  `
      '\G(?^|,)(("(?[^,"\n]*?)"(?=,|$))|(?".*?(?

    Test Output

    ---- data ---
    
    id string               notes                       number
    -- ------               -----                       ------
    1  hello world.         classic                     123   
    2  a comma, is in here  test data 1                 345   
    3  ,a comma, is in here test data 2                 346   
    4  a comma, is in here, test data 3                 347   
    5  a comma, is in here, test data 4...              347   
    6  hello world2.        classic                     123   
    
    ---- quotedData ---
    "id","string","notes","number"
    "1","hello world.","classic","123"
    "2","a comma, is in here","test data 1","345"
    "3",",a comma, is in here","test data 2","346"
    "4","a comma, is in here,","test data 3","347"
    "5","a comma, is in here,","test data 4
    with a newline","347"
    "6","hello world2.","classic","123"
    
    ---- fixedData ---
    id,string,notes,number
    1,hello world.,classic,123
    2,"a comma, is in here",test data 1,345
    3,",a comma, is in here",test data 2,346
    4,"a comma, is in here,",test data 3,347
    5,"a comma, is in here,","test data 4
    with a newline","347"
    6,hello world2.,classic,123
    
    ---- e:\test.csv ---
    id,string,notes,number
    1,hello world.,classic,123
    2,"a comma, is in here",test data 1,345
    3,",a comma, is in here",test data 2,346
    4,"a comma, is in here,",test data 3,347
    5,"a comma, is in here,","test data 4
    with a newline","347"
    6,hello world2.,classic,123
    

提交回复
热议问题