使用PowerShell sls(选择字符串)vs grep vs findstr [英] Using PowerShell sls (Select-String) vs grep vs findstr

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

问题描述

有人可以澄清sls(选择字符串)与grep和findstr相比如何工作吗?

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

grep: grep< pattern> files.txt

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

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

grep示例: grep 搜索文字 * .log cat * .log | grep搜索文字

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

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

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

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

所以,我的想法是好的,我需要将Get-Service的输出转换为字符串以使用PowerShell Cmdlet。无效(或无法达到我期望的方式):

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

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

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

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

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

一般而言,我应该如何将对象变成一个字符串,以便它可以操纵信息(就像 Get-Service | findstr Sec 可以很容易地进行操作一样)?

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

如果有人能在上面阐明它们的组合方式,以便我可以更多地使用sls,将不胜感激。特别是 Get-Service |出线sls Sec 确实返回了东西,而不是我所期望的东西(它是在搜索 s, e和 c的每个字符,而是返回很多-这不会如果我认为是,则非常直观)?

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

推荐答案

使用输出字符串默认情况下,它将管道输入对象(在这种情况下为服务对象的数组)转换为单个字符串。幸运的是, -Stream 开关允许每行输出为单个字符串。关于区分大小写, Select-String 支持 -CaseSensitive 开关。

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

在两种情况下, Select -String 使用正则表达式(使用 -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(选择字符串)vs grep vs findstr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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