简体   繁体   English

如何巩固Windows Powershell选择字符串?

[英]How to consolidate windows powershell select-string?

this command works, but wondering if there's a more concise, terse way to do it. 该命令有效,但想知道是否还有更简洁的方法。 Eg, fewer pipes, fewer commands, fewer switches, etc. Mainly, fewer characters. 例如,更少的管道,更少的命令,更少的开关等。主要是更少的字符。

get-childitem *  -recurse | select-string  "some string"  | select -expandproperty Path | select -uniq

thx! 谢谢!

You don't need to do the select unique. 您不需要选择唯一。 select-string 's -List parameter will make sure you only get one match per file. select-string-List参数将确保每个文件只有一个匹配项。

(ls * -r|sls 'foo' -lis).path

Or, modified per comments below: 或者,根据以下注释进行修改:

(ls -r|sls 'foo' -lis).path

You could start with some of the built in aliases: 您可以从一些内置别名开始:

ls -r | sls 'some string' | % path | select -u

Detail: 详情:

ls -> Get-ChildItem
-r -> -Recurse
sls -> Select-String
% -> ForEach-Object
select -> Select-Object
-u -> -Unique

You can specify a path, with wildcards, to Select-String , so this can be pretty short. 您可以使用通配符指定Select-String的路径,因此这可能很短。

(Select-String 'pattern' *.*).Path | Select -Unique

If you use the alias sls for Select-String it gets even shorter. 如果将别名sls用于Select-String则它会更短。

(sls 'pattern' *.*).Path | Select -Unique

Edit: As pointed out, the above does not do recursive searching. 编辑:如前所述,上面不进行递归搜索。 To accomplish that you'd have to do something very similar to what @RachelDuncan suggested. 要实现这一点,您必须执行与@RachelDuncan建议的非常相似的操作。

ls * -r | sls 'pattern' | Select -Exp Path -U

comparing the two answers given so far, i started with @TheMadTechnician's because it uses fewer pipes-- typing a pipe on a US keyboard requires pressing the SHIFT key, so fewer pipes = fewer SHIFTs = fewer keystrokes. 比较到目前为止给出的两个答案,我从@TheMadTechnician开始,因为它使用的管道较少-在美国键盘上键入管道需要按SHIFT键,因此,较少的管道=较少的SHIFT =较少的击键。

Then i removed the * from the ls command, that's unnecessary. 然后我从ls命令中删除了* ,这是不必要的。

Like @Rachel Duncan's answer, i converted all commands and switches to lowercase, they don't need to be uppercase-- that eliminates more SHIFT presses. 就像@Rachel Duncan的答案一样,我将所有命令和开关都转换为小写字母,而不必将它们转换为大写字母,从而省去了更多的SHIFT印刷机。

And i removed all spaces surrounding the pipes, they are unnecessary 我删除了管道周围的所有空间,它们是不必要的

@Rachel Duncan: 46 chars, 3 SHIFTS @Rachel Duncan:46个字符,3个移位

ls -r | sls 'some string' | % path | select -u

@The Mad Technician: 45 chars, 6 SHIFTS @疯狂的技术人员:45个字符,6个移位

ls * -r | sls 'pattern' | Select -Exp Path -U

@Johny Why: 40 chars, 2 SHIFTS @Johny Why:40个字符,2个移位

ls -r|sls 'textarea'|select -exp path -u

Still, this seems like it should be easier yet. 不过,这似乎应该还容易一些。 Before i mark Solved, can anyone offer a yet terser method? 在我标记为已解决之前,谁能提供更简洁的方法?

Perhaps using Windows command-line, instead of powershell? 也许使用Windows命令行而不是Powershell? (tho', i guess technically that would not answer the question, because i specified using powershell. Maybe stack would allow a command-line solution in the comments? :) (tho',从技术上讲,我猜不会回答这个问题,因为我使用powershell指定。也许堆栈允许注释中使用命令行解决方案?

here's the best answer, so far: 到目前为止,这是最好的答案:

(ls -r|sls 'foo' -list).path

i removed *, and added the missing t on -list. 我删除了*,并在-list上添加了缺失的t。

28 chars, 3 shifts 28个字符,3个班次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM