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)
运行此命令后,更新并保存文件,您将在控制台上看到更改.
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行并等待更多
Dan*_*ard 114
从PowerShell 3.0版开始,Get-Content cmdlet具有应该有用的-Tail参数.有关Get-Content的信息,请参阅technet库在线帮助.
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-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.
Run Code Online (Sandbox Code Playgroud)
Mik*_*erg 15
只是对以前答案的一些补充.为Get-Content定义了别名,例如,如果您习惯使用UNIX cat
,并且还有type
和gc
.而不是
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)
归档时间: |
|
查看次数: |
298249 次 |
最近记录: |