Getting multiple lines between two strings RRS feed

  • Question

  • Hi Experts,

    I am stuck in a code, my requirement is to get the lines between two strings, please suggest a regex pattern


    Device Name: test
    Status: Completed
    [PUSH_TERMLET] Action completed successfully...
    Pushing configuration file 'PUSH_TERMLET' for IDX 60504 with mechanism SSH
    ****** Enable Mode Results ******
    sh run | sec ip access-list standard customer_internal
    ip access-list standard customer_internal



    Here is the code that i am  trying 

    $content = Get-Content -Path "TestACL.txt"
    $device_list = $content | Select-String -Pattern "device name" -AllMatches
     foreach($temp in $device_list){
     $pattern = "(?<=$temp|permit)(.*)"
    $content | Select-String -Pattern $pattern

    Awaiting a solution!

    Wednesday, May 15, 2019 7:53 AM


All replies

  • Here are some examples and instructions:


    Wednesday, May 15, 2019 7:58 AM
  • If you're looking for the lines starting with "permit" you should include this in your pattern in the first place. ;-)

    Live long and prosper!


    • Edited by BOfH-666 Wednesday, May 15, 2019 8:00 AM
    Wednesday, May 15, 2019 7:59 AM
  • Or simply:

    Get-Content TestACL.txt | Where{ $_ -match '^permit' }


    Wednesday, May 15, 2019 8:04 AM
  • Try this:

    $x = get-content C:\temp\testacl.txt -raw
    $x -match '(?ms)^ip access-list.*?$(.*)^zap'

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 15, 2019 3:17 PM
  • try also select-string


    Captures the specified number of lines ....


    Wednesday, May 15, 2019 4:03 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Monday, May 20, 2019 6:53 AM
  • Hi Lee

    It worked with below code:

    $Content = (Get-Content "C:\temp\TestACL.txt") -join ' '
    $variable = "test#"
    $Output = ($Content  | Select-String -Pattern "(?:permit )(.*)(?= "zapesxr01#")").Matches.Value

    Komal Bachchani

    Tuesday, May 28, 2019 7:59 AM
  • Hi,
    Thank you for your update and please "mark it as answer" to help other community members find the helpful reply quickly. 
    As always, if there is any question in future, we warmly welcome you to post in this forum again. We are happy to assist you! 

    Have a nice day!
    Best Regards,
    Lee  Li

    Just do it.

    Tuesday, May 28, 2019 8:02 AM