【问题标题】:Select a string from a tailing log从拖尾日志中选择一个字符串
【发布时间】:2015-09-08 08:22:38
【问题描述】:

我有一个应用程序日志,我正在尝试为其编写一个批处理文件,该文件将跟踪日志并返回包含“队列大小”的字符串,以便可以显示更新的队列大小。基本上相当于 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 batch-file grep tail


    【解决方案1】:

    不,PowerShell 命令在更新日志时不会继续读取日志。 PowerShell 无法真正处理此任务,因此您最好获取 Unix tail 命令的 Windows 端口(例如来自 GnuWin32UnxUtils)并将其与批处理 find 命令一起使用:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        应该这样做:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-18
          相关资源
          最近更新 更多