This topic contains 4 replies, has 3 voices, and was last updated by
July 8, 2017 at 3:25 pm #74486
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:
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.
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 #74515KeymasterPoints: 1,619Rank: Community Hero
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 #74542
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"
Here is the input.txt file:
policy_name Blacklist Senders/policy_name
policy_name Safelist Senders/policy_name
/policy_member policy_name Blocked Recipients/policy_name
July 10, 2017 at 3:00 am #74548ParticipantPoints: 48Rank: Member
What format is the input text or where is it generated? It almost Looks like XML?
July 10, 2017 at 1:09 pm #74563
Yes its XML.
The topic ‘Search for string in a text file recursively’ is closed to new replies.