Asked by:
Extracting Multiple strings from a text file

Question
-
Hi,
I need to search for a string "policy_name" in a TEXT file and extract all the corresponding "policy_member"s . I have to do this recursively till end of file.
With this command it only gives me the "policy_name":
$matches = Select-String -Pattern “policy_name” -Path C:\Temp\input.txt
I could search for the string "policy_member" , but then I will not know the corresponding "policy_name". Thats the reason I need something like a while/for loop and get for the POSITION of the "policy_name" when I find it. Then use the POSITION to determine the start of the following "policy_member"
Here is a snippet of the input TXT file:
policy_name Blacklist Senders policy_name_end
policy_member
@3.org
@Atos.com
@Ms.1and1.com
policy_member_end
policy_name Safelist Senders policy_name_end
policy_member
@engps.com
@sl.com
@cq.com
policy_member_end policy_name Blocked Recipients policy_name_end
policy_member
mail08@gmail.com
private@persal-email.org
chiefreply@gmail.com
policy_member_end
Thursday, July 20, 2017 3:03 PM
All replies
-
maybe it helps you
Example 8: Find lines before and after a match
PS C:\> $F = Select-String -Path "audit.log" -Pattern "logon failed" -Context 2, 3 PS C:\> $F.count PS C:\> ($F)[0].context | Format-List
Chris
Thursday, July 20, 2017 5:26 PM -
Thanks Chris. Good Start
Any ideas on how I would be able to loop through and extract the components of each line using the For loop
$F = Select-String -Path "C:\Temp\IronPortEval\config_clus.txt" -Pattern "policy_name" -Context 2, 3 $MaxCount = $f.count For ($i=0; $i -le $MaxCount; $i++) { }
- Edited by hdsouza Saturday, July 22, 2017 3:46 PM
Saturday, July 22, 2017 3:41 PM -
Something like this?
$F = Select-String -Path "C:\Temp\IronPortEval\config_clus.txt" -Pattern "policy_name(.*?)policy_name_end" -AllMatches $MaxCount = $f.count For ($i=0; $i -le $MaxCount; $i++) { $_ }
Gastone Canali >http://www.armadillo.it
Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHereSunday, July 23, 2017 8:34 PM -
Not sure about the detail structure of the input file. Based on your snippet of INPUT txt file, I would convert it to xml and process as xml. :) just my 10c
$xmlPolicies = [xml]([string[]]'<root>'+ ((gc 'c:\temp\input.txt') -replace '\s*(policy_(name|member))_end', '</$1>' -replace'policy_(name|member)([^>]|$)\s*', '<policy_$1>' -replace '(<policy_name>)', '<pol>$1' -replace '(</policy_member>)', '$1</pol>') + '</root>' ) $hashPolicies = @{} $xmlPolicies.root.ChildNodes | % { $hashPolicies[$_.Policy_name]=$_.policy_member}
rgds,
az
Monday, July 24, 2017 9:56 AM -
Hi,
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.
Best Regards,
FrankPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Friday, August 11, 2017 4:42 AM