我有一个巨大的 XML 文件 (0.5 GB),没有换行符.我希望能够在不打开整个文件的情况下查看前 200 个字符.有没有办法用 PowerShell 做到这一点?
I've got a huge XML file (0.5 GB), with no line breaks. I want to be able to look at, say, the first 200 characters without opening the whole file. Is there a way to do this with PowerShell?
推荐答案PowerShell Desktop (up to 5.1)
您可以像这样使用 Get-Content 在字节级别读取:
PowerShell Desktop (up to 5.1)
You can read at the byte level with Get-Content like so:
$bytes = Get-Content .\files.txt -Encoding byte -TotalCount 200 [System.Text.Encoding]::Unicode.GetString($bytes)如果日志文件是 ASCII,您可以将其简化为:
If the log file is ASCII you can simplify this to:
[char[]](Get-Content .\files.txt -Encoding byte -TotalCount 200)PowerShell Core 6.0 及更新版本
PowerShell Core 不支持 byte 编码.它已被 -AsByteStream 参数取代.
PowerShell Core 6.0 and newer
PowerShell Core doesn't support byte encoding. It's been replaced by -AsByteStream parameter.
$bytes = Get-Content .\file.txt -AsByteStream -TotalCount 200 [System.Text.Encoding]::Unicode.GetString($bytes)更多推荐
使用 PowerShell 获取大文件的前 n 个字符
发布评论