johny why
johny why

Reputation: 2211

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!

Upvotes: 0

Views: 413

Answers (5)

kb0
kb0

Reputation: 532

You don't need to do the select unique. select-string's -List parameter will make sure you only get one match per file.

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

Or, modified per comments below:

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

Upvotes: 2

johny why
johny why

Reputation: 2211

here's the best answer, so far:

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

i removed *, and added the missing t on -list.

28 chars, 3 shifts

Upvotes: 0

johny why
johny why

Reputation: 2211

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.

Then i removed the * from the ls command, that's unnecessary.

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.

And i removed all spaces surrounding the pipes, they are unnecessary

@Rachel Duncan: 46 chars, 3 SHIFTS

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

@The Mad Technician: 45 chars, 6 SHIFTS

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

@Johny Why: 40 chars, 2 SHIFTS

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

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36322

You can specify a path, with wildcards, to Select-String, so this can be pretty short.

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

If you use the alias sls for Select-String it gets even shorter.

(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.

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

Upvotes: 0

user5859111
user5859111

Reputation:

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

Upvotes: 1

Related Questions