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.
9 Answers
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
PS) new-alias grep findstr PS) C:\WINDOWS> ls | grep -I -N exe 105:-a--- 2006-11-02 13:34 49680 twunk_16.exe 106:-a--- 2006-11-02 13:34 31232 twunk_32.exe 109:-a--- 2006-09-18 23:43 256192 winhelp.exe 110:-a--- 2006-11-02 10:45 9216 winhlp32.exe PS) grep /? 7I'm not familiar with grep but with Select-String you can do:
Get-ChildItem filename.txt | Select-String -Pattern <regexPattern> You can also do that with Get-Content:
(Get-Content filename.txt) -match 'pattern' 2I had the same issue trying to find text in files with powershell. I used the following - to stay as close to the Linux environment as possible.
Hopefully this helps somebody:
PowerShell:
PS) new-alias grep findstr PS) ls -r *.txt | cat | grep "some random string" Explanation:
ls - lists all files -r - recursively (in all files and folders and subfolders) *.txt - only .txt files | - pipe the (ls) results to next command (cat) cat - show contents of files comming from (ls) | - pipe the (cat) results to next command (grep) grep - search contents from (cat) for "some random string" (alias to findstr) Yes, this works as well:
PS) ls -r *.txt | cat | findstr "some random string" 1So I found a pretty good answer at this link:
But essentially it is:
Select-String -Path "C:\file\Path\*.txt" -Pattern "^Enter REGEX Here$" This gives a directory file search (*or you can just specify a file) and a file-content search all in one line of PowerShell, very similar to grep. The output will be similar to:
doc.txt:31: Enter REGEX Here HelloWorld.txt:13: Enter REGEX Here 2This question already has an answer, but I just want to add that in Windows there is Windows Subsystem for Linux WSL.
So for example if you want to check if you have service named Elasicsearch that is in status running you can do something like the snippet below in powershell
net start | grep Elasticsearch
but select-String doesn't seem to have this option.
Correct. PowerShell is not a clone of *nix shells' toolset.
However it is not hard to build something like it yourself:
$regexes = Get-Content RegexFile.txt | Foreach-Object { new-object System.Text.RegularExpressions.Regex $_ } $fileList | Get-Content | Where-Object { foreach ($r in $regexes) { if ($r.IsMatch($_)) { $true break } } $false } 1Maybe?
[regex]$regex = (get-content <regex file> | foreach { '(?:{0})' -f $_ }) -join '|' Get-Content <filespec> -ReadCount 10000 | foreach { if ($_ -match $regex) { $true break } } I find out a possible method by "filter" and "alias" of PowerShell, when you want use grep in pipeline output(grep file should be similar):
first define a filter:
filter Filter-Object ([string]$pattern) { Out-String -InputObject $_ -Stream | Select-String -Pattern "$pattern" } then define alias:
New-Alias -Name grep -Value Filter-Object final, put the former filter and alias in your profile:
$Home[My ]Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Restart your PS, you can use it:
alias | grep 'grep'
==================================================================
relevent Reference
alias: Set-Aliasenter link description here New-Aliasenter link description here
Filter(Special function)enter link description here
Profiles(just like .bashrc for bash):enter link description here
out-string(this is the key)enter link description here:in PowerShell Output is object-basedenter link description here,so the key is convert object to string and grep the string.
Select-Stringenter link description here:Finds text in strings and files