使用 PowerShell sls (Select-String) vs grep vs findstr [英] Using PowerShell sls (Select-String) vs grep vs findstr

查看:35
本文介绍了使用 PowerShell sls (Select-String) vs grep vs findstr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与 grep 和 findstr 相比,有人可以澄清 sls(Select-String)的工作原理吗?

grep: grep <模式>文件.txt

sls: sls <模式>文件.txt(sls 的默认参数位置是模式然后是文件)

grep 示例:grep "search text" *.log ;cat *.log |grep "搜索文本"

sls 示例:sls "搜索文本" *.log ;cat *.log |grep "搜索文本"

顺便说一句,所有 PowerShell Cmdlet 都不区分大小写,这与通常始终区分大小写的 Linux 工具不同,但 findstr 等旧工具也区分大小写,但 findstr 可以在 PowerShell 中使用,并且可以在以下情况下工作sls 没有,例如:Get-Service |findstr "Sec" (这没有问题!),但是当我们尝试以类似的方式使用 sls 时 Get-Service |sls "Sec" 我们什么也得不到(可能这会失败,因为 sls 使用字符串,但 Get-Service 返回一个对象,所以这是可以理解的 - 但是 findstr 做了什么,因为它可以将输出视为字符串?).

所以,我的想法是好的,我需要将 Get-Service 的输出变成一个字符串以使用 PowerShell Cmdlet",但这不起作用(或者不是以我期望的方式):

获取服务 |外串 |sls "Sec"(给出结果,但很奇怪)

(Get-Service).ToString() |sls "Sec" (.ToString() 只返回 "System.Object[]")

一般而言,我应该如何将对象转换为字符串,以便它可以操纵信息(就像 Get-Service | findstr "Sec" 可以轻松做到这一点)?p>

如果有人能阐明上述内容如何组合在一起,我将不胜感激,以便我可以更多地使用 sls.特别是 Get-Service |外串 |sls "Sec" 确实返回了一些东西,而不是我期望的东西(它是否搜索s"和e"和c"的每个字符,所以返回很多 - 如果所以在我看来)?

解决方案

默认使用 Out-String 时,会将管道输入对象(本例中为服务对象数组)转换为单个字符串.幸运的是,-Stream 开关允许将每一行输出为单个字符串.关于区分大小写,Select-String 支持 -CaseSensitive 开关.

# 用于不区分大小写的正则表达式匹配获取服务 |串外流 |选择字符串秒"# 对于区分大小写的正则表达式匹配获取服务 |串外流 |选择字符串Sec"-CaseSensitive# 对于区分大小写的非正则表达式匹配获取服务 |串外流 |选择字符串 "Sec" -CaseSensitive -SimpleMatch

在任何一种情况下,Select-String 都使用正则表达式(使用 -SimpleMatch 开关进行字符串匹配)对每个输入字符串进行模式匹配并输出整个字符串与模式相匹配.因此,如果您只通过管道将一个包含多行的字符串导入其中,那么所有行都将在成功匹配时返回.

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)?

解决方案

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.

这篇关于使用 PowerShell sls (Select-String) vs grep vs findstr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆