PowerShell equivalent to grep -f

Fordio picture Fordio · Mar 4, 2013 · Viewed 281.7k times · Source

I'm looking for the PowerShell equivalent to grep --file=filename. If you don't know grep, filename is a text file where each line has a regular expression pattern you want to match.

Maybe I'm missing something obvious, but Select-String doesn't seem to have this option.

Answer

Frode F. picture Frode F. · Mar 4, 2013

The -Pattern parameter in Select-String supports an array of patterns. So the one you're looking for is:

Get-Content .\doc.txt | Select-String -Pattern (Get-Content .\regex.txt)

This searches through the textfile doc.txt by using every regex(one per line) in regex.txt