Powershell 外文件 : Force encoding to Ascii

标签 powershell character-encoding

我正在尝试使用 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/

相关文章:

powershell - 将数组匹配到多个正则表达式(来自数组)

mysql - 如何让 SELECT 输出转义的 "\0"字符

perl - 如何检测和报告对于使用 Perl 进行交换不合法的 Unicode 代码点?

linux - shell 错误地读取在 Windows 中创建的文件

php - 如何在 vbulletin 数据库读取中将字符集设置为 utf

powershell - powershell命令中的等效/s/V"/qn

java - 如何在 java 中运行以下 Powershell 命令?

git - 为什么ls在GitShell中工作,而ls -a或ls -la无法工作?

powershell - VSCode 覆盖 PowerShell ExecutionPolicy

MySQL 字符编码更改。是否保留了数据完整性?