Skip to content

Instantly share code, notes, and snippets.

@ticapix
Last active November 26, 2015 07:13
Show Gist options
  • Select an option

  • Save ticapix/9b86e86c94af0a153b67 to your computer and use it in GitHub Desktop.

Select an option

Save ticapix/9b86e86c94af0a153b67 to your computer and use it in GitHub Desktop.
grep in powershell
# ORIGIN http://weblogs.asp.net/whaggard/powershell-script-to-find-strings-and-highlight-them-in-the-output
# Find-String.ps1
# Wrapper around dir | select-string which will highlight the pattern in the results
param ( [string] $pattern = ""
, [string] $filter = "*.*"
, [switch] $recurse = $false
, [switch] $caseSensitive = $false)
if ($pattern -eq $null -or $pattern -eq "") { Write-Error "Please provide a search pattern!" ; return }
$regexPattern = $pattern
if($caseSensitive -eq $false) { $regexPattern = "(?i)$regexPattern" }
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
# Write the line with the pattern highlighted in red
function Write-HostAndHighlightPattern([string]$inputText)
{
$index = 0
while($index -lt $inputText.Length)
{
$match = $regex.Match($inputText, $index)
if($match.Success -and $match.Length -gt 0)
{
Write-Host $inputText.SubString($index, $match.Index - $index) -nonewline
Write-Host $match.Value.ToString() -ForegroundColor Red -nonewline
$index = $match.Index + $match.Length
}
else
{
Write-Host $inputText.SubString($index) -nonewline
$index = $inputText.Length
}
}
}
# Do the actual find in the files
Get-ChildItem -recurse:$recurse -filter:$filter |
Select-String -caseSensitive:$caseSensitive -pattern:$pattern |
foreach {
Write-Host "$($_.Path | Resolve-Path -Relative)($($_.LineNumber)): " -nonewline
Write-HostAndHighlightPattern $_.Line
Write-Host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment