751
General Software Discussion / Re: Extract REGEX matches from multiple text files
« Last post by 4wd on August 03, 2018, 10:11 PM »$items - an arbitrarily named variable
= - sign signifying equality
Get-ChildItem
Thus $items now equals an array of files in the current folder that match *.txt
$items[0] = firstfile.txt
$items[1] = secondfile.txt
etc
etc
etc
$items.Count - total number of matching files found
for(){} - a for loop, $i is a variable that gets incremented by 1 every loop until the total number of matching files is reached
Thus loop through all the files in the array performing the following on every file:
Select-String -Path $items[$i] -Pattern $regex -AllMatches
Search each file for matching RegEx pattern, get all matches.
| % { $_.Matches } | % { $_.Value } >> $outfile
RegEx matches are piped into a ForEach-Object loop, (shorthand notation). For each regex match, pipe it's value to the output file in append mode.
Don't actually need to escape the " in the RegEx either:
Will also work.
Same as the 6 lines above without assigned variables or a for loop:
= - sign signifying equality
Get-ChildItem
Thus $items now equals an array of files in the current folder that match *.txt
$items[0] = firstfile.txt
$items[1] = secondfile.txt
etc
etc
etc
$items.Count - total number of matching files found
for(){} - a for loop, $i is a variable that gets incremented by 1 every loop until the total number of matching files is reached
Thus loop through all the files in the array performing the following on every file:
Select-String -Path $items[$i] -Pattern $regex -AllMatches
Search each file for matching RegEx pattern, get all matches.
| % { $_.Matches } | % { $_.Value } >> $outfile
RegEx matches are piped into a ForEach-Object loop, (shorthand notation). For each regex match, pipe it's value to the output file in append mode.
Don't actually need to escape the " in the RegEx either:
Code: PowerShell [Select]
- $regex = '<dsf:tsdfgd trsdfge="urn:x-ssdfgs-dfg-com:isdfgc/tg4r3e-i4d" id="OsdfgsdfD">'
Same as the 6 lines above without assigned variables or a for loop:
Code: PowerShell [Select]

Recent Posts