views:

74

answers:

1

I would like to translate the following Unix 1 Liner to PowerShell.

Synopsis of the command: This command will search recursively form the PWD (pressent working directory) for any file with the extenstion .jsp, and look inside the file for a simple string match of 'logoutButtonForm'. If it finds a match, it will print the file name and the text that it matched.

find . -name "*.jsp" -exec grep -aH "logoutButtonForm" {}\;

I am new to power shell and have done some googling/binging but have not found a good answer yet.

+5  A: 
ls . -r *.jsp | Select-String logoutButtonForm -case

I tend to prefer -Filter over -Include. Guess I never trusted the -Exclude/-Include parameters after observing buggy behavior in PowerShell 1.0. Also, -Filter is significantly faster than using -Include.

Keith Hill
Both this command and the one by Johannes Produce the same result. This command runs much faster. ( I am amazed at how fast.)
Q Boiler
Yikes. Now *I'm* already falling into the trap of including useless `foreach`​s. `-Include` is a habit, somehow. I always tried `gci -r *.foo` and obviously it doesn't do what one would expect so I never arrived at `gci . -r *.foo`. As for `Select-String` – this is about the first time I ever used it. Most of the time `(gc meh) -match 'foo'` is enough for me. I think `Select-String` needs a short alias by default. Something like `grep` might be fitting, I think ;-) ... and now I can't even delete my answer anymore.
Joey
Yeah in `gci -r *.foo` the positional `*.foo` is assigned to the -Path parameter. By sticking in the `.` first, `*.foo` now gets assigned to the next positional parameter which is `-Filter`. In PSCX, we create what I would call a PowerShell canonical style alias of `sls` but `grep` would be good for the UNIX style alias.
Keith Hill
Yeah, I kinda see what happened with the parameters there, I just never dug deeply enough to truly understand it. I guess, my style fits me well enough for everyday use and I don't do much with the file system anyway. As for aliases, I think having both PS-style aliases and one for the corresponding Unix command is actually beneficial. But I also think PowerShell sometimes still struggles finding its actual audience – Unix/Linux converts will definitely appreciate the `grep` alias :-)
Joey