How To Grep Text Files With Powershell Grep or Select-String Cmdlet In Windows?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/grep-text-files-powershell-grep-select-string-cmdlet-windows/
PINTEREST
PINTEREST
INSTAGRAM

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 tool.

Help

Help about Select-String can be get with the following command.

Help

Help

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

  • -Pattern specifies the string we are searching for

poet.txt is the file we search in.

Search String In A File

Search String In A File

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.

Search String In Multiple Files

Search String In Multiple Files

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.

LEARN MORE  Python Type Function with Examples

Search Files Recursively

Search Files Recursively

Case Sensitive Search

By default given strings are searched case insensitive. We can change this behaviour by using -CaseSensitve option like below.

Case Sensitive Search

Case Sensitive Search

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.

Match Regular Expression

Match Regular Expression

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.

Match Whole Word

Match Whole Word

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.

Display N Lines Before Match

Display N Lines Before Match

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.

LEARN MORE  Pgrep and Pkill Command Tutorial With Examples For Linux

Display N Lines After Match

Display N Lines After Match

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.

Display N Line Before and After Match

Display N Line Before and After Match

Highlighting Matches

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.

Invert Match or Not Match

Invert Match or Not Match

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.

Count Of Matches

Count Of Matches

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/grep-text-files-powershell-grep-select-string-cmdlet-windows/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)