none
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

    Input:

    Device Name: test
    Status: Completed
    Results:
    [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
     permit 204.79.49.116
     permit 159.12.130.0 0.0.0.31
     permit 10.108.30.0 0.0.0.255
     permit 10.108.141.0 0.0.0.127
     permit 10.108.172.0 0.0.0.255
     permit 10.108.160.0 0.0.0.31
    zapesxr01#

    Output:

     permit 204.79.49.116
     permit 159.12.130.0 0.0.0.31
     permit 10.108.30.0 0.0.0.255
     permit 10.108.141.0 0.0.0.127
     permit 10.108.172.0 0.0.0.255
     permit 10.108.160.0 0.0.0.31

    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){
    
     $temp.sp
    
     $pattern = "(?<=$temp|permit)(.*)"
    
    $content | Select-String -Pattern $pattern
    
     }

    Awaiting a solution!

    Wednesday, May 15, 2019 7:53 AM

Answers

All replies

  • Here are some examples and instructions: https://www.regular-expressions.info/


    \_(ツ)_/

    Wednesday, May 15, 2019 7:58 AM
    Moderator
  • 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!

    (79,108,97,102|%{[char]$_})-join''


    • 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
    Moderator
  • Try this:

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


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

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

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6

    -Context

    Captures the specified number of lines ....



    Chris

    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,

    Lee



    Just do it.

    Monday, May 20, 2019 6:53 AM
    Moderator
  • 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
    Moderator