How To Grep Text Files With Powershell Grep or Select-String Cmdlet In Windows?
Linux provides tool named grep for filter text data or output according to given string or regular expression. This tool is popular amongst Linux system administrators. On the other side Windows operating systems generally lacks this tool and its functionality up to Powershell. Powershell provides
Select-String commandlet to provide similar features and options the Linux grep tool provides. In this tutorial we will look different use cases with examples of
Select-String can be get with the following command.
PS> get-help Select-String
Search String In A File
One of the simplest usage and most used feature is simply searching given string in a file. We will provide following options
-Patternspecifies the string we are searching for
poet.txt is the file we search in.
PS> Select-String -Pattern EX .\poet.txt
Search String In Multiple Files
In previous example we have searched given string in a single file but real world problems are more than that. We can search string in multiple files by providing file name or extension with the help asterisk. In this example we will search in all text files by specifying
*.txt file name.
PS> Select-String -Pattern EX *.txt
Search Files Recursively
Now the most advanced file specification is searching files recursively. Recursively searching will look given string in all current folder and al subfolders. We will provide
Get-ChildItem command to provide files recursively to the
Select-String command like below.
PS> Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern EX
Case Sensitive Search
By default given strings are searched case insensitive. We can change this behaviour by using -CaseSensitve option like below.
PS> Select-String -Pattern EX -CaseSensitive *.txt
Match Regular Expression
Regular expression provides to define more rich and structured string expressions.
Select-String command also supports regular expressions. We can provide regular expressions into pattern too. In this example we will use regular expression
E.*E to match string.
PS> Select-String -Pattern "EX.*E" poet.txt
Match Whole Word
By default given search term or string is looked partially or on whole words. If we need to match whole word which is surrounded by white spaces we should put white spaces around the search term. We will search
case search term as a whole word.
PS> Select-String -Pattern " case " poet.txt
Display N Lines Before Match
If we are looking some part of the text and need to see previous lines of the matches we can provide
-Context option whit the number of lines we want to print.
PS> Select-String -Pattern "case" -Context 2, poet.txt
Display N Lines After Match
We will use
-Context options again but we will provide after part of the line numbers. In this example we will print 3 lines after the match.
PS> Select-String -Pattern "case" -Context ,3 poet.txt
Display N Line Before and After Match
Now we will provide both parameters to the
-Context where before and after line numbers will be provided. In this example we will print 1 lines before match and 2 lines after match in a single shot.
PS> Select-String -Pattern "case" -Context 1,2 poet.txt
In a result where there are a lot of match and text seeking the match specifically very hard. Highlighting the match will make the job easier.
Invert Match or Not Match
Another useful feature is printing not matched lines or invert match. This is like logic
NOT operation. We will use
-NotMatch option tho show non matched lines.
PS> Select-String -Pattern "case" -NotMatch poet.txt
Count Of Matches
We may need to count the matches. If there are a lot of match counting them one by one is very trivial task. We can use returned matches
count property to print count of matched lines.
PS> $Result = Select-String -Pattern "case" -NotMatch poet.txt