这里的文档和字符串在 Windows 上都使用 UNIX 0x0A 行结尾,而不是 0x0D0A。我怎样才能让它们成为 Windows 行结尾?
PS C:\> $s = @"
>> now
>> is
>> the
>> "@
PS C:\> $s
now
is
the
PS C:\> $s | Format-Hex
Label: String (System.String) <7B93DCA4>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 6E 6F 77 0A 69 73 0A 74 68 65 now�is�the
PS C:\> $s2 = "
>> Now
>> is
>> the
>> "
PS C:\> $s2
Now
is
the
PS C:\> $s2 | Format-Hex
Label: String (System.String) <33E42D9F>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 0A 4E 6F 77 0A 69 73 0A 74 68 65 0A �Now�is�the�
PS C:\> $PSVersionTable.PSVersion.ToString()
7.3.7
您有两种方法可以做到这一点:
您可以使用dos2unix。
或者您可以使用正则表达式。
有多种正则表达式语法,但对于 powershell 它只是
-replace '(?<!\r)\n', "\`r\`n"