Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PowerShell sls (Select-String) vs grep vs findstr

Could someone clarify how sls (Select-String) works compared to grep and findstr?

grep: grep <pattern> files.txt

sls: sls <pattern> files.txt (default parameter position for sls is pattern then file)

grep examples: grep "search text" *.log ; cat *.log | grep "search text"

sls examples: sls "search text" *.log ; cat *.log | grep "search text"

As an aside, all PowerShell Cmdlets are case-insensitive, unlike Linux tools which are generally always case-sensitive but also older tools like findstr which are case sensitive too, but findstr can be used in PowerShell, and works in situations where sls does not, for example: Get-Service | findstr "Sec" (this works without a problem!), but when we try to use sls in a similar way Get-Service | sls "Sec" we get nothing (presumably this fails because sls works with strings, but Get-Service returns an object, so that's understandable - but what is findstr doing then in that it can see the output as a string?).

So, my thinking is "ok, I need to make the output from Get-Service into a string to work with PowerShell Cmdlets", but that doesn't work (or not in a way that I would expect):

Get-Service | Out-String | sls "Sec" (gives results, but odd)

(Get-Service).ToString() | sls "Sec" (.ToString() just returns "System.Object[]")

How in general should I turn an object into a string so that it can manipulate the information (in the same way that Get-Service | findstr "Sec" can do so easily)?

Would appreciate if someone could clarify how things fit together in the above so that I can make more use of sls. In particular, Get-Service | Out-String | sls "Sec" does return stuff, just not the stuff I was expecting (is it searching for each character of "s" and "e" and "c" so is returning lots - that would not be very intuitive if so in my opinion)?

like image 890
YorSubs Avatar asked Dec 07 '19 19:12

YorSubs


People also ask

What is the equivalent of grep in PowerShell?

The simplest PowerShell equivalent to grep is Select-String. The Select-String cmdlet provides the following features: Search by regular expressions (default); Search by literal match (the parameter -Simple);

Can you grep in PowerShell?

When you need to search through a string or log files in Linux we can use the grep command. For PowerShell, we can use the grep equivalent Select-String . We can get pretty much the same results with this powerful cmdlet. Select-String uses just like grep regular expression to find text patterns in files and strings.

How do I select multiple strings in PowerShell?

To search for multiple matches in each file, we can sequence several Select-String calls: Get-ChildItem C:\Logs | where { $_ | Select-String -Pattern 'VendorEnquiry' } | where { $_ | Select-String -Pattern 'Failed' } | ...

What is select-String in PowerShell?

Select-String uses the Pattern parameter to search each file for the string PowerShell. From the PowerShell command line, the $A variable contents are displayed. There's a line that contains two occurrences of the string PowerShell.


1 Answers

When you use Out-String by default, it turns the piped input object (an array of service objects in this case) into a single string. Luckily, the -Stream switch allows each line to be output as a single string instead. Regarding case-sensitivity, Select-String supports the -CaseSensitive switch.

# For case-insensitive regex match
Get-Service | Out-String -Stream | Select-String "Sec"

# For case-sensitive regex match
Get-Service | Out-String -Stream | Select-String "Sec" -CaseSensitive

# For case-sensitive non-regex match
Get-Service | Out-String -Stream | Select-String "Sec" -CaseSensitive -SimpleMatch

In either case, Select-String uses regex (use the -SimpleMatch switch to do a string match) to pattern match against each input string and outputs the entire string that matched the pattern. So if you only pipe into it a single string with many lines, then all lines will be returned on a successful match.

like image 120
AdminOfThings Avatar answered Nov 15 '22 01:11

AdminOfThings