PowerShell equivalent to grep -f
Solution 1
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
Solution 2
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 /?
Solution 3
I'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'
Solution 4
I 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"
Solution 5
So I found a pretty good answer at this link: https://www.thomasmaurer.ch/2011/03/powershell-search-for-string-or-grep-for-powershell/
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
Related videos on Youtube
Fordio
Updated on December 16, 2020Comments
-
Fordio over 3 years
I'm looking for the PowerShell equivalent to
grep --file=filename
. If you don't knowgrep
, 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. -
joon over 10 yearsAlso, PowerShell tab completion will make commands properly capitalized, so it is not hard to input.
-
Jeroen over 8 yearsExtended this to e.g.
dir *.cs -Recurse | sls "TODO" | select -Unique "Path"
. Thx for the excellent pointer. -
Frode F. over 7 yearsI never understood how this got so many votes. It's doesn't even answer the question..
-
Elijah W. Gagne over 7 yearsWhat I like about this answer is that
findstr
works the most likegrep
on Linux does.Select-String
is great for working with objects, but sometimes you just want to match on strings. -
dee-see over 7 years@joon A couple of years late, but I'd add that Powershell is case insensitive so the capitalization of commands is not an issue.
-
joon over 7 years@Vache of course - I think my comment above is a reply to another comment, but it seems the comment is gone anymore.
-
anishpatel almost 7 yearsIt is worth noting that
findstr
is not native to PowerShell, but Command Prompt. -
Rag about 6 yearsHe wants to load the pattern from a file.
-
Lennon over 5 yearsThis findstr command was already available to me, and it worked just like unix GREP.
-
Kellen Stuart almost 5 yearsThis is a bad answer.
Select-String -Pattern somestring
is much cleaner -
cody.tv.weber over 3 yearsIf that is what he is asking, there are other answers that are similar to mine, so I feel like the question may not be worded super clear of desired answer. But after rereading it, I agree that he probably is wanting to pipe regex from a file into select string.
-
Ken Seehart over 3 yearsThat's pretty good, but I also want the name of the file where it was found
-
pixis about 3 years@dee-see A couple of years late, but I'd add that Windows is case insensitive so the capitalization of commands, paths,... is not an issue. :)
-
skaveesh over 2 years@FrodeF. that's because this question is the first thing comes up when we search for "powershell grep equivalent". :)
-
ruffin over 2 yearsWorth mentioning that
-I
is "insensitive" and-N
includes the line number, and that in Windows you'd normally use/i
and/n
. Also note that the/r
option for regex is on by default. All parameters forfindstr
are currently described here. As best as I can tell, you usefind
(nostr
) to avoid regular expressions. -
рüффп about 2 yearsGood to know the alias is
sls
which is faster to write -
Dan about 2 yearsSelect-String isn't very good at doing line-based context, which grep is great at.