ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Extract REGEX matches from multiple text files

<< < (3/22) > >>

wraith808:
Its pretty standard poweshell, and there are a lot of tutorials online that will explain the commands to you.  Just as a start, # is a line comment, so everything after that on the line is just documentation.

4wd:
$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:

--- Code: PowerShell ---$regex = '<dsf:tsdfgd trsdfge="urn:x-ssdfgs-dfg-com:isdfgc/tg4r3e-i4d" id="OsdfgsdfD">'Will also work.

Same as the 6 lines above without assigned variables or a for loop:

--- Code: PowerShell ---gci *.txt | % { sls $_.Name -Pattern '<dsf:tsdfgd trsdfge="urn:x-ssdfgs-dfg-com:isdfgc/tg4r3e-i4d" id="OsdfgsdfD">' -a | % { $_.Matches } | % { $_.Value } >> K:\out.txt }

Ath:
Also, I need to append to the output file several regex matches/returns, how do I do that?
Also, if I specify a regex match, how do I specify what I want to be returned from this match?
-kalos (August 03, 2018, 05:15 PM)
--- End quote ---
Why did you leave these rather important 'details' out in your original question?

4wd:
Why did you leave these rather important 'details' out in your original question?-Ath (August 04, 2018, 07:03 AM)
--- End quote ---

Um .... because that's what he normally does ... it always takes at least a week, (sometimes longer or never), before all pertinent information is obtained ...

wraith808:
Why did you leave these rather important 'details' out in your original question?-Ath (August 04, 2018, 07:03 AM)
--- End quote ---

Um .... because that's what he normally does ... it always takes at least a week, (sometimes longer or never), before all pertinent information is obtained ...
-4wd (August 04, 2018, 07:37 AM)
--- End quote ---

And you're a lot more patient than I, in regards to someone not wanting to do the due diligence after you've given them the solution.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version