Created
November 27, 2012 22:33
-
-
Save aauren/4157632 to your computer and use it in GitHub Desktop.
PowerShell Grep Equivilent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Select-StringSpecial { | |
param( | |
[string] $query | |
) | |
Begin { | |
# Executes once before first item in pipeline is processed | |
} | |
Process { | |
# Executes once for each pipeline object | |
$args = @{} | |
$tempArg = "" | |
foreach($param in $query.split(" ")) | |
{ | |
if("" -ne $tempArg) | |
{ | |
if(-1 -ne $param.IndexOf(",")) | |
{ | |
$param = $param.Split(",") | |
} | |
$args.Add($tempArg, $param) | |
$tempArg = "" | |
continue | |
} | |
if(-1 -eq $param.IndexOf("-")) | |
{ | |
$args.Add("pattern",$param) | |
} | |
else | |
{ | |
$tempArg = $param | |
} | |
} | |
($_ | Out-String).split("`n") | Select-String @args | |
} | |
End { | |
# Executes once after last pipeline object is processed | |
} | |
} | |
Set-Alias ss Select-StringSpecial | |
Set-Alias grep Select-StringSpecial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a fast alternative for linux/unix grep (for files) you could also try the
psitems
powershell module. It includes apsgrep
command (alias forFind-ItemContent
) that works similar to the linux/unixgrep
command (the basic way, only a few parameters of original grep included).(above command searches for pattern test in all files in current directory recursively (
-R
) and highlights the results (-H
))Fore more information check the README.