How to follow a log until a string is matched in PowerShell

Viewed 115

I would like to be able to do something similar to this is PowerShell

docker logs -f my-container | grep -q "string-to-match"

i.e. to follow a log file and when a string is matched you stop follow the file. My idea was to try something like this

docker logs -f my-container | Select-String "string-to-match".

I know it's not complete but I can't figure out how to make it work. I also did try to use WSL2 like this

docker logs -f my-container | wsl grep -q "string-to-match".

but it keeps following the log even after a match has been found!

A WSL-solution would solve my problem but a native PowerShell-solution would be preferable!

1 Answers

I tried with ugrep.exe in PowerShell:

docker logs -f my-container | ugrep -q "string-to-match"

and that appears to work fine as it stops at the first match. This can also be done with ugrep -m1 "string-to-match" to report the line that matched, since -m1 stops searching further after the first hit.

Related