我必须查看一个大文件(典型大小为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的有效实现吗?
当前回答
有许多有效的答案,但是,没有一个与linux中的tail具有相同的语法。下面的函数可以存储在$Home\Documents\PowerShell\Microsoft.PowerShell_profile中。Ps1的持久性(请参阅powershell概要文档了解更多详细信息)。
这允许你调用…
tail server.log
tail -n 5 server.log
tail -f server.log
tail -Follow -Lines 5 -Path server.log
这与Linux语法非常接近。
function tail {
<#
.SYNOPSIS
Get the last n lines of a text file.
.PARAMETER Follow
output appended data as the file grows
.PARAMETER Lines
output the last N lines (default: 10)
.PARAMETER Path
path to the text file
.INPUTS
System.Int
IO.FileInfo
.OUTPUTS
System.String
.EXAMPLE
PS> tail c:\server.log
.EXAMPLE
PS> tail -f -n 20 c:\server.log
#>
[CmdletBinding()]
[OutputType('System.String')]
Param(
[Alias("f")]
[parameter(Mandatory=$false)]
[switch]$Follow,
[Alias("n")]
[parameter(Mandatory=$false)]
[Int]$Lines = 10,
[parameter(Mandatory=$true, Position=5)]
[ValidateNotNullOrEmpty()]
[IO.FileInfo]$Path
)
if ($Follow)
{
Get-Content -Path $Path -Tail $Lines -Wait
}
else
{
Get-Content -Path $Path -Tail $Lines
}
}
其他回答
只是对之前答案的补充。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
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
可以从这个GitHub存储库下载为Windows编译的所有UNIX命令:https://github.com/George-Ogden/UNIX
关于这个主题,我有一个关于多个文件的有用提示。
使用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 }