2016-04-01 12 views
0

Powershellを使用して、TABで区切られたログファイルのセットを正規化しようとしています。ここでPowershell Out-File:Asciiへのエンコードの強制

は、現在のスクリプトです:

(Get-ChildItem *.csv) |%{ 
#Store the name of the file & date 
$Name = $_.basename 
$FileDate = $_.CreationTime 
#Prepends the following to each message: unique Identifer, Hostname, date 
(Get-Content $_.fullname) -replace "^","AR-LOG|$Name|$FileDate|"| 
#Replaces the TAB delimeter with a Pipe delimeter 
Foreach-Object {$_ -replace ' ','|'} | 
#Appends the resulting message in ascii 
Out-File -append -FilePath normalized.log -Encoding ascii 

入力&出力はここで見ることができるの抜粋:

http://pastebin.com/uaQadYUC

どのように私はASCIIであることが、出力ファイルを強制的にすることはできませんユニコードのいくつかのタイプ?

***編集:(?)さらに、トラブルシューティングは、入力ファイルが実際に窓-1252はエンコードされていることを明らかにし、明らかにGET-コンテンツをネイティブに扱うことができない

答えて

2

あなたはOUT-にエンコーディングフラグを使用することができるはずです... | Out-File -encoding ascii myfile.txtのようにファイルします。 appendを使用している場合は、すべての追加で同じエンコードが使用されていることを確認してください。そうしないと、使用できないファイルが作成されます。

+0

はい、これは私がスクリプトで行っていることです(表示できます)が、動作しているようには見えません。 –

0

ReadAllTextメソッドで遊ぶことができますか?ファイル全体を1つの文字列に格納します。 Get-Contentは値を文字列の配列として格納します。ここで、array valueはファイルの行です。

(Get-ChildItem *.csv) |%{ 
#Store the name of the file & date 
$Name = $_.basename 
$FileDate = $_.CreationTime 
#Prepends the following to each message: unique Identifer, Hostname, date 
([IO.File]::ReadAllText($_.fullname)) -replace "^","AR-LOG|$Name|$FileDate|" 
#Replaces the TAB delimeter with a Pipe delimeter 
-replace ' ','|' | 
#Appends the resulting message in ascii 
Out-File -append -FilePath normalized.log -Encoding ascii 
関連する問題