searching text files. RRS feed

  • Question

  • Hi Scripting guys,

    I've got another PowerShell question.

    I have a txt file containing a load of stuff, I want to search that text file for a string THEN look for a specific string that occurs after that. 

    so I want to search a file find a specific sting then perform another search that only looks after the location of the first string match in the file.

    How might one do that?



    • Moved by Bill_Stewart Monday, November 23, 2015 10:41 PM This is not "scripts on demand"
    Friday, September 18, 2015 9:06 AM

All replies

  • Help select-string -full

    Look at the examples.


    Friday, September 18, 2015 9:40 AM
  • You're a bit vague about the format and the pattern of your text file. But, maybe you could use a combination of Select-String and Regex.Match like in this scripting:

    $path = "$env:tmp\Temp\textfile.txt"
    $pattern = "(?<=.*\bthe\b)\s\w+\b"
    Select-String -path $path -Pattern "der" | % {
        foreach ($i in [Regex]::Matches($_, $pattern,  [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)) 
    It looks for any appearance of the word "the" and selects the respective word after it.


    Friday, September 18, 2015 10:19 AM