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

http://tailforwin32.sourceforge.net/

and

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

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


当前回答

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

gc filename -wai -ta 10

其他回答

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

使用PowerShell 5.2 (Win7和Win10)跟踪单个日志文件(如Linux中的'tail -f')很容易(只需使用“Get-Content MyFile -Tail 1 -Wait”)。然而,同时查看多个日志文件似乎很复杂。PowerShell 7。x+然而,我发现了一个简单的方法,使用“Foreach-Object - parallel”。这将同时执行多个“Get-Content”命令。例如:

Get-ChildItem C:\logs\*.log | Foreach-Object -Parallel { Get-Content $_ -Tail 1 -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.

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

gc filename -wai -ta 10

使用Powershell V2及以下版本时,get-content读取整个文件,因此对我来说没有任何用处。下面的代码可以满足我的需要,尽管字符编码可能存在一些问题。这实际上是tail -f,但是可以很容易地修改它以获得最后x个字节,或者如果您想向后搜索换行符,则可以修改它以获得最后x行。

$filename = "\wherever\your\file\is.txt"
$reader = new-object System.IO.StreamReader(New-Object IO.FileStream($filename, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [IO.FileShare]::ReadWrite))
#start at the end of the file
$lastMaxOffset = $reader.BaseStream.Length

while ($true)
{
    Start-Sleep -m 100

    #if the file size has not changed, idle
    if ($reader.BaseStream.Length -eq $lastMaxOffset) {
        continue;
    }

    #seek to the last max offset
    $reader.BaseStream.Seek($lastMaxOffset, [System.IO.SeekOrigin]::Begin) | out-null

    #read out of the file until the EOF
    $line = ""
    while (($line = $reader.ReadLine()) -ne $null) {
        write-output $line
    }

    #update the last max offset
    $lastMaxOffset = $reader.BaseStream.Position
}

我在这里找到了大部分代码。

也许现在回答已经太迟了,但是试试这个

Get-Content <filename> -tail <number of items wanted> -wait