1

我编写了一个 PowerShell 脚本,使用以下代码将文件从 Windows 转换为 Unix。

while ($line = $tempfile.ReadLine()) {
    $outstream.Write($line + "`n")
}

如果数据文件中没有特殊字符,它可以正常工作。现在,如果数据文件中有一些特殊字符,它们将被转换如下:

test‡testtest¿½test
test,testtest¿½test

任何建议如何在不转换任何字符的情况下实现它?

注意:我不想使用dos2unix此 exe,因为服务器上不存在此 exe。所以我需要批处理脚本或PowerShell中的解决方案。

4

2 回答 2

0

你试过[io.file]吗?好像没有这些问题。

$txt = [io.file]::ReadAllText('E:\temp\dos.txt') -replace "`r`n","`n"
[io.file]::WriteAllText('e:\temp\unix.txt', $txt)

从这里: 如何将文件从 DOS 转换为 Unix

于 2019-02-28T07:37:48.813 回答
0

不确定这是您的问题,但我认为这只是编码。

尽管 :

$tempfile = [System.IO.StreamReader]("d:\temp\test.txt")
$tempfile.ReadLine()

你可以试试 :

Get-Content 'D:\temp\test.txt' -encoding UTF8

然后使用:

$tempfile = New-Object  System.IO.StreamReader "d:\temp\test.txt", $([System.Text.Encoding]::UTF8)
$tempfile.ReadLine()

可用编码:

ASCII ; 统一码;BigEndianUnicode ; UTF32 ; UTF7 ; UTF8 ; 默认(对于 OEM)

于 2019-02-28T07:06:33.513 回答