Windows Powershell中的Unix尾部等效命令

mut*_*gan 331 windows powershell tail

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

http://tailforwin32.sourceforge.net/

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

对我来说,不允许使用第一种替代方案,第二种方案是缓慢的.有没有人知道PowerShell的尾部有效实现.

rav*_*nth 468

将该-wait参数与Get-Content一起使用,该参数显示添加到文件中的行.此功能在PowerShell v1中出现,但由于某些原因未在v2中很好地记录.

这是一个例子

Get-Content -Path "C:\scripts\test.txt" -Wait
Run Code Online (Sandbox Code Playgroud)

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

  • 有趣.我原以为所有存在的参数也都出现在帮助中,但是`man gc -par wait`告诉我没有参数.但我认为这并没有解决OP的问题,因为他们要求`tail`,而不是`tail -f`和一个有效的实现.由于这个也在返回最后一行之前读取完整文件,因此对于他们期望的文件大小来说是痛苦的. (15认同)
  • 奇怪的是,当我以某种方式访问​​日志文件时(例如在Windows资源管理器中选择它)时,-Wait只显示新行.当新行被写入我的文件时,Tail会提供更新.使用-Wait,我可以快速打开PowerShell窗口,在写入文件时不显示新行.如果我然后弹出并在Windows资源管理器中单击该文件,突然PowerShell"唤醒"并赶上其余的行.这是一个错误吗? (15认同)
  • 我知道它是不久前的,但是这需要写入文件的过程在Get-Content工作之前打开,追加,关闭它.如果写入过程永远不会关闭文件,那么它将无法正常工作,而tail -f则不然. (11认同)
  • @Joey -Wait是一个动态参数,仅适用于FileSystem提供程序.GC可以在任何实现该API的提供程序上使用.我知道发现这些文档的唯一方法是在相应的提供程序路径中使用(gcm Get-Content).Parameters.不要使用别名"gc",因为动态参数不会显示. (7认同)
  • 仅供参考,这就是PSCX中Get-FileTail(别名尾部)实现的功能.如果您很好奇,可以查看源代码:http://pscx.codeplex.com/SourceControl/changeset/view/78514#1358075 (5认同)

Geo*_*uer 179

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

Get-Content ./log.log -Tail 10
Run Code Online (Sandbox Code Playgroud)

获取文件的最后10行

Get-Content ./log.log -Wait -Tail 10
Run Code Online (Sandbox Code Playgroud)

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

  • 我刚刚使用它,它以这种格式`Get-Content.\ test.txt -Wait -Tail 1 (4认同)

Dan*_*ard 114

从PowerShell 3.0版开始,Get-Content cmdlet具有应该有用的-Tail参数.有关Get-Content的信息,请参阅technet库在线帮助.

  • 请注意 - PS 3.0在Windows XP和Vista中不可用. (4认同)

Joh*_*ood 21

我使用了这里给出的一些答案,但只是提了一下

Get-Content -Path Yourfile.log -Tail 30 -Wait 
Run Code Online (Sandbox Code Playgroud)

会在一段时间后咀嚼记忆.一位同事在最后一天留下了这样的"尾巴",最高达800 MB.我不知道Unix尾部的行为是否相同(但我对此表示怀疑).因此可以用于短期应用,但要小心它.


Evo*_*ile 19

对于答案来说可能为时已晚,但是,尝试这个

Get-Content <filename> -tail <number of items wanted> -wait
Run Code Online (Sandbox Code Playgroud)


Rom*_*min 18

PowerShell社区扩展(PSCX)提供Get-FileTailcmdlet.它看起来像是一个适合该任务的解决方案.注意:我没有尝试使用非常大的文件,但描述说它有效地拖尾内容,它是专为大型日志文件设计的.

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.
Run Code Online (Sandbox Code Playgroud)

  • 版本2.0需要很长时间才能显示1GB csv文件的10个最后一行,与`Get-Content [filename] |不同 Select-Object-Last 10`它不能被中止 (7认同)

Mik*_*erg 15

只是对以前答案的一些补充.为Get-Content定义了别名,例如,如果您习惯使用UNIX cat,并且还有typegc.而不是

Get-Content -Path <Path> -Wait -Tail 10
Run Code Online (Sandbox Code Playgroud)

你可以写

# 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
Run Code Online (Sandbox Code Playgroud)


小智 5

关于这个主题,我有一个关于多个文件的有用提示。

使用 PowerShell 5.2(Win7 和 Win10)跟踪单个日志文件(如 Linux 中的“tail -f”)非常简单(只需使用“Get-Content MyFile -Tail 1 -Wait”)。然而,同时查看多个日志文件似乎很复杂。然而,在 PowerShell 7.x+ 中,我通过使用“Foreach-Object -Parrallel”找到了一种简单的方法。这会同时执行多个“获取内容”命令。例如:

Get-ChildItem C:\logs\*.log | Foreach-Object -Parallel { Get-Content $_ -Tail 1 -Wait }
Run Code Online (Sandbox Code Playgroud)