PowerShell Grep
-
Select-String
Cmdlet - Typical Grep Task Using PowerShell
- Using PowerShell Select-String to Find Patterns
Where do you go when developing PowerShell code whenever you need to scan for content within a string or a whole text file? You’re acquainted with the popular grep utility if you’ve used Linux a lot. The grep utility lets users find text using various parameters; however, it is not available in Windows. So in this scenario, we have the Select-String cmdlet in PowerShell.
Select-String
Cmdlet
Select-String checks for the initial match in each line by default, and then it shows the line number, file name, and text belonging to the matched line. Select-String may also work with multiple file encodings, for example, Unicode text, by determining the encoding type using the byte-order-mark (BOM). Select-String will presume it’s a UTF8 file if the BOM is missing.
Some uses of Select-String
cmdlet are explained below.
Typical Grep Task Using PowerShell
Let’ s start with an example. Let’s assume that we have a string with names and addresses, and also, the string is unstructured. We want to extract the names. So how are we going to do it will be explained below.
||Sara Peiris|| 37, De silva road, Panadura
--||Tim Gangster||-- 345, Yolks street,KL
==|Suz Maker|== 44 Main, Cydney, CA
The above text is assigned to the users
variable. So with the attempt will be searching for a name using the ‘Pattern’ parameter.
test | Select-String -Pattern 'Sara Peiris'
Output:
Here, we can understand that the Select-String method worked but didn’t return the specified one since we passed the whole string. Therefore let’s try passing single lines by breaking using the new-line character since each specific entry is given in a single line
PS> test = test -split "`n"
PS> test | Select-String -Pattern 'Sara Peiris'
Output:
Here we can see it returns a single line. Next, we should look into a way we can return multiple lines.
Using PowerShell Select-String to Find Patterns
Here, we need to find common patterns that are valid for all the lines. So if we take the previous example, you can see all the names are surrounded with |
and the names are separated by a space. So now, let’s take this pattern using the regex and send it with the ‘Pattern’ parameter.
Select-String
has used the regex to retrieve each line; after that, I’ll have to separate all the names. I don’t require the addresses for each at this moment. We will use the Matches
attribute of each matched object returned by Select-String to accomplish this.
PS> test | Select-String -Pattern '\|\w+ \w+\|' | foreach {$_.Matches}
Output:
Now you can see that the Value attribute has the names we require, but they are still surrounded by the |
character. That’s because the regex match included the |
character in the names.
The pipe characters must still be included in the filter, but we do not want them returned as matches. Regex groups are one method. The result you’d want to return is surrounded by parentheses, which indicate regex groups. In this situation, I’ll try again by enclosing the regex string that represents the first and last name.
PS> test | Select-String -Pattern '\|(\w+ \w+)\|' | foreach {$_.Matches}
Output:
The |
character is shown in the value, but whatsoever we can see the group has become {0,1}
which means Select-String has identified the group.
I’ll re-insert the reference in the for
each loop to see this group. Because each group attribute is an array, We can refer to the first member by enclosing it in parentheses and then using Value property.
PS> test | Select-String -Pattern '\|(\w+ \w+)\|' | foreach {$_.Matches.Groups[1].Value}
Output:
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.