Select 来自拖尾日志的字符串

Select a string from a tailing log

我有一个应用程序日志,我正在尝试为其编写一个批处理文件,该文件将跟踪日志和包含 "queue size" 的 return 字符串,以便可以显示更新队列大小。基本上 Windows 相当于:

tail -f app.log | grep "queue size"

根据我的阅读,我需要使用 Windows powershell。我设计了以下脚本:

powershell -command Select-String -Path C:\logs\app.log -Pattern "queue size"

这给了我以下错误:

Select-String : A positional parameter cannot be found that accepts
argument 'size'. At line:1 char:1
+ Select-String -Path C:\logs\app.log -Pattern queue size
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand

虽然目前这样行不通,但它会根据当前逻辑不断更新吗?

您需要将命令用双引号引起来,并为模式使用单引号:

powershell -command "Select-String -Path C:\logs\app.log -Pattern 'queue size'"

不,PowerShell 命令不会在日志更新时继续读取日志。 PowerShell 不能真正处理这个任务,所以你最好获取 Unix tail 命令的 Windows 端口(例如来自 GnuWin32 or UnxUtils)并将其与批处理一起使用 find命令:

tail -f C:\path\to\app.log | find "queue size"

应该这样做:

cat c:\path\to\app.log -tail 100 -wait | select-string "queue size"

cat 是 Get-Content 的别名... -wait 参数将使其等待日志更新。