Implementing UNIX Grep functionality in PowerShell


The grep UNIX command allows you to find lines in files that contain key words or phrases. With this command, it is possible to perform a quick search of a file or directory without having to look at each file via a text editor or the UNIX more command.

To achieve same functionality in PowerShell, we can use Select-String cmdlet as shown below:

dir -Recurse *.* | Select-String -Pattern "Clear" | Select -Unique path

This will list all the files with path that contains “Clear” as string inside it.

Below command line will look for all the patterns available in Pattern.txt and will list the matching files with the path.

dir -Recurse *.* | Select-String -Pattern (Get-Content .\Pattern.txt) | Select -Unique path

–End of Article–

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s