我必须查看一个大文件(典型大小为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的有效实现吗?
当前回答
只是对之前答案的补充。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 3.0版本开始,Get-Content cmdlet有一个-Tail参数,这应该会有所帮助。关于Get-Content,请参阅technet库的在线帮助。
只是对之前答案的补充。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
我使用了这里给出的一些答案,但只是提醒一下
Get-Content -Path Yourfile.log -Tail 30 -Wait
会在一段时间后咀嚼记忆。一个同事在过去的一天里留下了这样一个“尾巴”,它高达800 MB。我不知道Unix尾巴是否也有同样的行为(但我怀疑)。所以短期使用是可以的,但要小心。
我采用了@hajamie的解决方案,并将其包装到一个更方便的脚本包装器中。
我添加了一个选项,从文件结束之前的偏移量开始,因此您可以使用类似尾部的功能,从文件结束处读取一定量的数据。注意偏移量是以字节为单位的,而不是行。
你还可以选择继续等待更多内容。
示例(假设您将其保存为TailFile.ps1):
.\TailFile.ps1 -File .\path\to\myfile.log -InitialOffset 1000000
.\TailFile.ps1 -File .\path\to\myfile.log -InitialOffset 1000000 -Follow:$true
.\TailFile.ps1 -File .\path\to\myfile.log -Follow:$true
这是脚本本身……
param (
[Parameter(Mandatory=$true,HelpMessage="Enter the path to a file to tail")][string]$File = "",
[Parameter(Mandatory=$true,HelpMessage="Enter the number of bytes from the end of the file")][int]$InitialOffset = 10248,
[Parameter(Mandatory=$false,HelpMessage="Continuing monitoring the file for new additions?")][boolean]$Follow = $false
)
$ci = get-childitem $File
$fullName = $ci.FullName
$reader = new-object System.IO.StreamReader(New-Object IO.FileStream($fullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [IO.FileShare]::ReadWrite))
#start at the end of the file
$lastMaxOffset = $reader.BaseStream.Length - $InitialOffset
while ($true)
{
#if the file size has not changed, idle
if ($reader.BaseStream.Length -ge $lastMaxOffset) {
#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
}
if($Follow){
Start-Sleep -m 100
} else {
break;
}
}
尝试Windows Server 2003资源工具包工具
它包含一个tail.exe,可以在Windows系统上运行。
https://www.microsoft.com/en-us/download/details.aspx?id=17657