2011-07-14 4 views
1

キャッシングリターンを持つcsvファイルにフィールドがあり、電子メールと思っています。可能であれば、可能であれば異なる行ターミネーターを定義したいと思います!powershellでは、export-csvコマンドレットを使用して、標準キャリッジリターンとは対照的に、行ターミネータを定義する方法がありますか?

+0

サンプルファイルはありますか? – JPBlanc

+0

最大のCSVファイルのサイズはどれくらいですか? –

答えて

2

慣例として、csvフィールドの複数行フィールドは二重引用符で区切ります。( ")PowerShellはこれを行うか、あなた必要があります。今、このファイルをインポート

PS C:\> $ob = new-object PSObject | 
    add-member -memberType NoteProperty -name "header" -value "header1" -PassThru | 
    add-member -memberType NoteProperty -name "body" -value @" 
Body text 
with carriage returns 
"@ ` 
    -PassThru | 
    add-member -memberType NoteProperty -name "tail" -value "tail1" -PassThru 
PS C:\> $ob 

header         body         tail 
------         ----         ---- 
header1         Body text...       tail1 

PS C:\> $ob | Export-Csv \temp\ml.csv 
PS C:\> get-content \temp\ml.csv 
#TYPE System.Management.Automation.PSCustomObject 
"header","body","tail" 
"header1","Body text 
with carriage returns","tail1" 

はあなたの元を与える必要があります。

PS C:\> (import-csv C:\Temp\ml.csv).body 
Body text 
with carriage returns 
1

存在しないようです。 Reflectorでコマンドのコードを見ると、最後に\ r \ nを使用するTextWriterのWriteLineメソッドを使用して行が書き込まれます。

+0

Aww、これは残念です... csvを編集する方法や問題を回避する方法に関する提案はありますか? – Jerome

0

実際にあなたがそのような私は、カンマを使用して、行を分類し-Delimiterを使用することができます"、"行を分類する...

import-csv "C:\Path" -Delimiter "," 
1

は、ここに1つのソリューションです:

function Remove-LineBreaks { 
    [CmdletBinding()] 
    param (
     [Parameter(ValueFromPipeline=$true)] 
     [psobject]$InputObject #csv row 
     , 
     [Parameter()] 
     [string]$Replacement = '' 
    ) 
    process { 
     $InputObject | %{$_ -replace "`n", $Replacement} 
    } 
} 

clear-host 
$example = 1..3 | %{new-object -TypeName PSObject -Property @{ 
    Index=$_ 
    Name=("I'm No {0:00}" -f $_) 
    [email protected]" 
this is some multiline text 
for item index $_ 
blah blah blah 
"@ 
}} 

write-host 'With Line Breaks' -ForegroundColor Green 
$example | convertto-csv -NoTypeInformation 
write-host 'Without Line Breaks' -ForegroundColor Green 
$example | convertto-csv -NoTypeInformation | Remove-LineBreaks -Replacement '; ' 

あなたはその後、最後に| ConvertFrom-CSVを追加することによって除去そのパラメータラインのすべてを持っていたオブジェクトに戻って変換することができ、または出力が追加することにより、ファイルにすることができます| set-content -Path '.\filename.csv'

関連する問題