使用 PowerShell 获取大文件的前 n 个字符 [英] Get the first n characters of a large file with PowerShell
本文介绍了使用 PowerShell 获取大文件的前 n 个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个巨大的 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 个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文