我必须查看一个大文件(典型大小为500MB-2GB)的最后几行。我正在寻找一个相当于Unix命令尾Windows Powershell。一些可供选择的方法是,

http://tailforwin32.sourceforge.net/

and

Get-Content [filename] | Select-Object -Last 10

对于我来说,第一种选择是不允许使用的,而第二种选择是很慢的。有人知道PowerShell的tail的有效实现吗?


当前回答

尝试Windows Server 2003资源工具包工具

它包含一个tail.exe,可以在Windows系统上运行。

https://www.microsoft.com/en-us/download/details.aspx?id=17657

其他回答

只是对之前答案的补充。Get-Content定义了别名,例如,如果您习惯UNIX,您可能喜欢cat,还有type和gc。所以与其

Get-Content -Path <Path> -Wait -Tail 10

你可以写

# Print whole file and wait for appended lines and print them
cat <Path> -Wait
# Print last 10 lines and wait for appended lines and print them
cat <Path> -Tail 10 -Wait

对于那些以“打字越少越好”为原则的管理员来说,下面是我能找到的最短版本:

gc filename -wai -ta 10

对Get-Content使用-wait参数,它将显示添加到文件中的行。这个特性在PowerShell v1中就有了,但是由于某种原因在v2中没有很好地记录。

这里有一个例子

Get-Content -Path "C:\scripts\test.txt" -Wait

运行此命令后,更新并保存该文件,您将在控制台上看到更改。

为了完整起见,我将提到Powershell 3.0现在在Get-Content上有一个-Tail标志

Get-Content ./log.log -Tail 10

获取文件的最后10行

Get-Content ./log.log -Wait -Tail 10

获取文件的最后10行并等待更多

另外,对于那些*nix用户,请注意大多数系统将cat别名为Get-Content,因此这通常是可行的

cat ./log.log -Tail 10

尝试Windows Server 2003资源工具包工具

它包含一个tail.exe,可以在Windows系统上运行。

https://www.microsoft.com/en-us/download/details.aspx?id=17657