Examples:
# (special note: normally, the powershell escape character is `, but in
# regexps it's \, except if what you're trying to escape is a dollar sign;
# then '\$' doesn't work and the escape sequence is '[$]'.)
# grep "searchstring" filename.txt
select-string filename.txt -pattern "searchstring" -caseSensitive | %{ $_.line }
# grep -i "searchstring" filename.txt
select-string filename.txt -pattern "searchstring" | %{ $_.line }
# grep -in --with-filename "searchstring" filename.txt
select-string filename.txt -pattern "searchstring"
# cat filename.txt | grep -i "searchstring"
# (showing that it works on piped input, and the screwey 'readcount' option
# from get-content)
gc -readcount 2048 .\filename.txt | select-string "searchstring"
Some things that don't work:
This is tempting, but doesn't work -- because "-readcontent" returns blocks of
2048 lines at a time (in this example), the matching will be whole blocks at a
time and any match within the block will return the entire block (including
many non-matching lines):
gc -readcount 2048 .\bigfile.txt | where {$_ -match "searchstring"}
I'm actually not sure why this works (and doesn't return non-matching
garbage); I'd have thought it was the same as the above, but apparently not:
gc -readcount 2048 .\bigfile.txt | ForEach-Object {$_ -match "searchstring"}
This, however, doesn't work, and I'm not sure what it's actually doing:
gc -readcount 2048 .\bigfile.txt | ForEach-Object {
if ($_ -match "searchstring") {
# do other stuff with $_
# -- this fails and seems to only operate on a few lines
}
}