0

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!

0

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, better! So far, this one's the winner, at 28 chars, 4 shifts :) But, you can remove *, and correct missing t on -list. improved version: (ls -r|sls 'foo' -list).path 28 chars, 3 shifts.
Aha! but did you realize the 't' was missing on purpose. 😀 Powershell only requires you give enough of the parameter to make in unambiguous. With sls -li, you get this error: Select-String : Parameter cannot be processed because the parameter name 'li' is ambiguous. Possible matches include: -LiteralPath -List. So sls -lis is as short as the select-string -list command can possibly be.
1

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

1 Comment

hi, i regret not giving points to everyone, because everyone in this thread contributed to an ever-improving solution :)
0

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

2 Comments

You are quite right, I missed that they wanted it to be recursive.
great answers! hard to pick the best: @TheMadTechnician's answer has fewer pipes, but more typing. Hrm... we can eliminate the * in ls
0

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

4 Comments

Have you considered "consolidating" the command by simply wrapping it in a function with a short name? This way you'd could get down to just 1 character + the input pattern
yes, i have. but first looking for a built-in method.
One could argue that defining a function/cmdlet is the "built-in" method for invoking (semi-)complex operations with concise syntax :-)
i hear you, @MathiasR.Jessen. I agree, in fact. A user-defined alias hides the 'complexity' of scripts or chained native functions, which is great-- except when it's hiding inefficient or badly written code-- or hiding the developer's inadequate knowledge of native fx. This thread is improving my knowledge. My plan is to define my own alias on my own machine, after whittling down the most concise native method. Alas, i'm not necessarily using my own machine at all times....
0

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

1 Comment

sadly, since kb0 did not come back and fix his answer, i'll have to post the corrected edit as a new answer. i wanted to give it to ya, kb0! I tried to edit it, but Stack does not allow edits under 6 characters meta.stackoverflow.com/questions/251580/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.