我必须查看一个大文件(典型大小为500MB-2GB)的最后几行。我正在寻找一个相当于Unix命令尾Windows Powershell。一些可供选择的方法是,
http://tailforwin32.sourceforge.net/
and
Get-Content [filename] | Select-Object -Last 10
对于我来说,第一种选择是不允许使用的,而第二种选择是很慢的。有人知道PowerShell的tail的有效实现吗?
我必须查看一个大文件(典型大小为500MB-2GB)的最后几行。我正在寻找一个相当于Unix命令尾Windows Powershell。一些可供选择的方法是,
http://tailforwin32.sourceforge.net/
and
Get-Content [filename] | Select-Object -Last 10
对于我来说,第一种选择是不允许使用的,而第二种选择是很慢的。有人知道PowerShell的tail的有效实现吗?
当前回答
可以从这个GitHub存储库下载为Windows编译的所有UNIX命令:https://github.com/George-Ogden/UNIX
其他回答
对Get-Content使用-wait参数,它将显示添加到文件中的行。这个特性在PowerShell v1中就有了,但是由于某种原因在v2中没有很好地记录。
这里有一个例子
Get-Content -Path "C:\scripts\test.txt" -Wait
运行此命令后,更新并保存该文件,您将在控制台上看到更改。
PowerShell Community Extensions (PSCX)提供了Get-FileTail cmdlet。这看起来是一个适合这项任务的解决方案。注意:我没有尝试它与非常大的文件,但描述说,它有效地尾部内容,它是为大型日志文件设计的。
NAME
Get-FileTail
SYNOPSIS
PSCX Cmdlet: Tails the contents of a file - optionally waiting on new content.
SYNTAX
Get-FileTail [-Path] <String[]> [-Count <Int32>] [-Encoding <EncodingParameter>] [-LineTerminator <String>] [-Wait] [<CommonParameters>]
Get-FileTail [-LiteralPath] <String[]> [-Count <Int32>] [-Encoding <EncodingParameter>] [-LineTerminator <String>] [-Wait] [<CommonParameters>]
DESCRIPTION
This implentation efficiently tails the cotents of a file by reading lines from the end rather then processing the entire file. This behavior is crucial for ef
ficiently tailing large log files and large log files over a network. You can also specify the Wait parameter to have the cmdlet wait and display new content
as it is written to the file. Use Ctrl+C to break out of the wait loop. Note that if an encoding is not specified, the cmdlet will attempt to auto-detect the
encoding by reading the first character from the file. If no character haven't been written to the file yet, the cmdlet will default to using Unicode encoding
. You can override this behavior by explicitly specifying the encoding via the Encoding parameter.
尝试Windows Server 2003资源工具包工具
它包含一个tail.exe,可以在Windows系统上运行。
https://www.microsoft.com/en-us/download/details.aspx?id=17657
我使用了这里给出的一些答案,但只是提醒一下
Get-Content -Path Yourfile.log -Tail 30 -Wait
会在一段时间后咀嚼记忆。一个同事在过去的一天里留下了这样一个“尾巴”,它高达800 MB。我不知道Unix尾巴是否也有同样的行为(但我怀疑)。所以短期使用是可以的,但要小心。
非常基本,但不需要任何附加模块或PS版本要求:
while ($true){清除主机;gc E:\test.txt | select -last 3;睡眠2}