algorhythm
algorhythm

Reputation: 3446

Select a string from a tailing log

I have an application log for which I am trying to write a batch file that will tail the log and return strings that contain "queue size" so that the updating queue size can be displayed. Basically the Windows equivalent of:

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

From what I've read I would need to use Windows powershell. I have devised the following script:

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

This gives me the following error:

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

Although this as it stands doesn't work, would it constantly update with the current logic?

Upvotes: 1

Views: 2278

Answers (3)

thinkbeforecoding
thinkbeforecoding

Reputation: 6778

this should do:

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

cat is an alias for Get-Content... The -wait parameter will make it wait for log updates.

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

No, the PowerShell command will not continue to read the log as it's being updated. PowerShell can't really handle this task, so you'd be better off grabbing a Windows port of the Unix tail command (e.g. from GnuWin32 or UnxUtils) and use it with the batch find command:

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

Upvotes: 2

Martin Brandl
Martin Brandl

Reputation: 58991

You need to wrap the command in double quotes and use single quotes for the pattern:

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

Upvotes: 1

Related Questions