我正在尝试使用 Powershell 规范化一组制表符分隔的日志文件。
这是当前的脚本:
(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 而不是某种类型的 unicode?
*** 编辑:进一步的故障排除表明输入文件实际上是 windows-1252 编码的,显然 Get-Content 无法本地处理(?)
最佳答案
您应该能够在输出文件上使用编码标志,如 ... | Out-File -encoding ascii myfile.txt
.如果您使用的是 append
, 确保所有附加使用相同的编码,否则您最终会得到一个无法使用的文件。
关于Powershell 外文件 : Force encoding to Ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36363227/