Skip to content

Instantly share code, notes, and snippets.

@thehandsomezebra
Created January 5, 2022 03:00
Show Gist options
  • Save thehandsomezebra/8426ad57f3bf1f2e829550602c9f5e39 to your computer and use it in GitHub Desktop.
Save thehandsomezebra/8426ad57f3bf1f2e829550602c9f5e39 to your computer and use it in GitHub Desktop.
Powershell - searching thru files using regex
$TestingLocation = "$HOME\test"
############## Making the files ###################
New-Item -Force -ItemType File -Path $TestingLocation\words.txt
"Beep`n123" | Add-Content -Path $TestingLocation\words.txt
New-Item -Force -ItemType File -Path $TestingLocation\wordstwo.txt
"YEAHBeep`n111" | Add-Content -Path $TestingLocation\wordstwo.txt
New-Item -Force -ItemType File -Path $TestingLocation\wordsthree.txt
"Boop`n55512" | Add-Content -Path $TestingLocation\wordsthree.txt
############### First tests? #####################
Select-String -Path "$TestingLocation\*.txt" -Pattern 'Beep' #works
Select-String -Path "$TestingLocation\*.txt" -Pattern 'ep`n1' #does not work
Select-String -Path "$TestingLocation\*.txt" -Pattern 'epn1' #does not work
Select-String -Path "$TestingLocation\*.txt" -Pattern 'Be{2}p' #works
Select-String -Path "$TestingLocation\*.txt" -Pattern 'Be{2}p\n1' #does not work
Select-String -Path "$TestingLocation\*.txt" -Pattern 'Be{2}p`r?`n1' #does not work
#https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.2
#This parameter is ignored when used in combination with the SimpleMatch parameter.
#If you wish to return all matches and the pattern that you are searching for contains regular expression characters,
#you must escape those characters rather than using SimpleMatch. See about_Regular_Expressions for more information about escaping regular expressions.
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.2
#ok so it won't work BECAUSE Select-String operates on each (stringified on demand) input object.
###### Attempting it a different way... ingest the txt, then run it thru with .Matches
### ALSO the magic is in doing it with -Raw
$fileContent = Get-Content $TestingLocation\words.txt -Raw
[regex]$regex1 = '(?m)(Beep\n1)'
$regex1.Matches($fileContent).value #outputs with the text that we search correctly as a match.
# https://ilovepowershell.com/2015/05/28/using-match-and-the-matches-variable-in-powershell/
#### testing -match like this...
"One" -match ".*" #says TRUE
$Matches="" #clear it out..
"One","Two","Three" | Where-Object {$_ -match "T.*"} | Foreach {$Matches[0]}
$Matches #Reports back with "Three"
#### and doing it like this with the RAW file content..
$fileContent -match "Be" #says TRUE
$fileContent -match "Be{2}p" #says TRUE
$fileContent -match "Be{2}p\n" #says TRUE
$fileContent -match "Be{2}p\n123" #says TRUE
## and now doing a Where-Object for it..
$Matches="" #clear it out..
$fileContent | Where-Object {$_ -match "B.*"} | Foreach {$Matches[0]}
$Matches #shows Beep
$Matches="" #clear it out..
$fileContent | Where-Object {$_ -match "LL.*"} | Foreach {$Matches[0]}
$Matches #Just wanted to test for false positives.. So this is outputting correctly null
########### Now attempting to search thru the folder, using some cool regex
dir -Path $TestingLocation\ -Filter *.txt -Recurse | %{$_.FullName} #make sure that this works to get a list of a filename
$Matches="" #clear it out..
Get-ChildItem "$TestingLocation" -Filter *.txt |
Foreach-Object {
$content = Get-Content $_.FullName
$content | Where-Object {$_ -match "B.*"} | Foreach {$Matches[0]}
}
## Yay! Shows Beep, Boop and Beep!
########## Now try for a linebreak
$Matches="" #clear it out..
Get-ChildItem "$TestingLocation" -Filter *.txt |
Foreach-Object {
$content = Get-Content $_.FullName -Raw
$content | Where-Object {$_ -match "B[e-o]{2}p\n"} | Foreach {$Matches[0]}
}
############## And do it a different way!!
$Matches="" #clear it out..
Get-ChildItem "$TestingLocation" -Filter *.txt |
Foreach-Object {
Write-Output $_.FullName
$content = Get-Content $_.FullName -Raw
if ( $content -match "B[eo]{2}p\n[0-9]{3}" )
{
Write-Output "We found a match!"
Write-Output $content
}
else
{
Write-Output "Sorry, nothing good here"
}
}
############## exclude the file with boop..only do the beeps
$Matches="" #clear it out..
Get-ChildItem "$TestingLocation" -Filter *.txt |
Foreach-Object {
Write-Output $_.FullName
$content = Get-Content $_.FullName -Raw
if ( $content -match "B[e]{2}p\n[0-9]{3}" )
{
Write-Output "We found a match!"
Write-Output $content
}
else
{
Write-Output "Sorry, nothing good here"
}
}
### And last but not least... we have ONE file that actually starts with the word Beep.. (the other one is YEAHBeep)
$Matches="" #clear it out..
Get-ChildItem "$TestingLocation" -Filter *.txt |
Foreach-Object {
Write-Output $_.FullName
$content = Get-Content $_.FullName -Raw
if ( $content -match "^B[e]{2}p\n[0-9]{3}" )
{
Write-Output "We found a match!"
Write-Output $content
}
else
{
Write-Output "Sorry, nothing good here"
}
}
########### ... powershell is not my fav... :/ ###############
@thehandsomezebra
Copy link
Author

btw, this worked for me on

Get-Host | Select-Object Version

Version      
-------      
5.1.22000.282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment