Author Posts

July 8, 2017 at 3:25 pm

Hi ,

I need to search for a string in a TEXT file (for Example "policy_member") and extract the content between strings. I need to do it recursively till end of the text content.
I could use:

Get-Content C:\input.txt

but I am not sure how to proceed.
I had written something similar in another language (as below) and was hoping I could write something similar with Powershell.

Please help.
Thank you

File_input = "C:\input.txt"
fs_Var = FileSize(File_input)
binbuf_Var = BinaryAlloc( fs_Var )
BinaryRead( binbuf_Var, File_input)
Offset_Var = 0
While @true
      Begin_String = BinaryIndexEx( binbuf_Var, Offset_Var, "", @FWDSCAN, 0) 
      if Begin_String == -1 then break
      End_String = BinaryIndexNc( binbuf_Var, Begin_String , "", @FWDSCAN) 
      Offset_Var = End_String
      Extracted_String = strtrim(BinaryPeekStr(binbuf_Var, Begin_String, End_String-Begin_String))
Endwhile
BinaryFree(binbuf_Var)

July 9, 2017 at 2:00 pm

PowerShell doesn't have the same string manipulation capabilities as some other scripting languages. You probably want to look into Regular Expressions; with a "capturing" expression, PowerShell's -match operator will generate a $matches collection that contains captures substrings. You can also look into Select-String to see if it meets your needs.

July 9, 2017 at 8:54 pm

Thanks Don. I already tried $matches but it will not help in this case.
I need to find “policy_name” and then get the corresponding "".
There are several "policy_names" and several "policy_member"s for the "policy_name"

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 , but then I will not know the corresponding "policy_member"

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"

Thanks
Hilario

Here is the input.txt file:

policy_name Blacklist Senders/policy_name
policy_member
sender @3.org/sender>
sender>@Atos.com/sender>
sender>@Ms.1and1.com/sender>
/policy_member
policy_name Safelist Senders/policy_name
policy_member
sender>@engps.com/sender>
sender>@sl.com/sender>
sender>@cq.com/sender>
/policy_member policy_name Blocked Recipients/policy_name
policy_member
receiver mail08@gmail.com/receiver
receiver>private@persal-email.org/receiver>
receiver>chiefreply@gmail.com/receiver>
/policy_member

July 10, 2017 at 3:00 am

What format is the input text or where is it generated? It almost Looks like XML?