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!

johny why
  • 2,047
  • 7
  • 27
  • 52

5 Answers5

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

kb0
  • 522
  • 3
  • 8
  • 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. – johny why Feb 12 '16 at 13:46
  • 1
    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. – kb0 Mar 04 '16 at 01:42
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
  • hi, i regret not giving points to everyone, because everyone in this thread contributed to an ever-improving solution :) – johny why Feb 11 '16 at 15:29
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
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
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? :)

johny why
  • 2,047
  • 7
  • 27
  • 52
  • 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 – Mathias R. Jessen Feb 10 '16 at 22:46
  • yes, i have. but first looking for a built-in method. – johny why Feb 11 '16 at 00:35
  • One could argue that defining a function/cmdlet *is* the "built-in" method for invoking (semi-)complex operations with concise syntax :-) – Mathias R. Jessen Feb 11 '16 at 00:40
  • 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.... – johny why Feb 11 '16 at 05:48
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

johny why
  • 2,047
  • 7
  • 27
  • 52
  • 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 http://meta.stackoverflow.com/questions/251580/what-is-the-reason-behind-the-6-character-minimum-for-suggested-edits – johny why Feb 16 '16 at 10:29