Select-String to return two lines (set to variables) and everything in between

Welcome Forums General PowerShell Q&A Select-String to return two lines (set to variables) and everything in between

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
9 months, 1 week ago.

  • Author
    Posts
  • #93211

    Participant
    Points: 0
    Rank: Member

    Here is my dilemma, I have been able to get the code below to search through all of the files in a directory, create new file names based on how I need them, and have even been able to get it to copy the lines of the variables $L and $zC, but I need to get all of the lines in between.

    The content between can vary in length and that is why I have it set up this way, but I cannot seem to get it to grab the data. Sample data is posted below with the code underneath.

    Thank you so much for all of your help and I eagerly await some expertise from those that actually know Powershell.

    Sample Lines from File

     ¨    ,"“
     
    
     "«
    
     ¨    +"› ' 
    
     "¬
     ©    *"›" 
    
     "¬
     « ' ' *"›'" '""­' ¬
    
    '
    
    )"›
    ' '“
    ""'” ¬
    "
    "
    ("›" ©
    "©"'
    "' ­
    

    ("›"
    ©
    ¨©""
    ¨ '•
    ª¦"'›ª
    ©¦©""¦
     ' “©¥'&"›©¨¦©""¦
     "§
    ¥'%"‹ §¨¤
    ¨#"¤ "§£'%"‹ §˜¤
    ˜#"¤ "
    ¥¢
    '$"Š
    ¥—
    ¢˜#"¢ "£  '#"Š£• –$"
      "
    ¡ž '#"‰¡”ž”%"ž "
    Ÿ›
    '""ˆ
    Ÿ'œ'&"œ "
    ›˜
    '!"‡›˜"'˜ "
    –'' "†–Œ“Œ)"“ "
    'Š=" ƒ!‡2Š2‡,"Š "
    C='
    “H"= '“
    C='
    “H'“= '“
    C='
    'H'“= '

    C=' “H'
    “> '•C='–H'•> ¯C=®H¯> ®C=­H®? ®C=¬H­B ­
    C=¬H­B ¬C=«H¬B «C=ªH¬B ªC=©H«B ©C=¨HªB ¨C=¦H¨B ¦C=¤H§B £C=¡H¤B  žC=œI B
    
    L
    D11
    A2
    3b840471087019604122088299
    1X1100005180036B194281001001
    1X1100005600041L184010
    1X1100005980041L184004
    1X1100006280041L170001
    1X1100006130041L184001
    1X1100006440041L170001
    1X1100006720041L184001
    1X1100006860041L184001
    1X1100007000041L184001
    1X1100007140041L170001
    1X1100007280041L170001
    1X1100007410041L184010
    1X1100007640041L184001
    1X1100007760041L184001
    1X1100005800041L184001
    1X1100006590041L170001
    1Y1100000830006gfx0
    Q0001
    E
    xCGgfx0
    zC

    Coding

    $Folder = "c:\Scan_and_Print\"
    Get-ChildItem -Path $Folder -Filter *.prn | Foreach {
    New-Item -Path $Folder -Name "$( $_.BaseName + "_graphic" ).txt"
    New-Item -Path $Folder -Name "$( $_.BaseName + "_dpl" ).txt" }
    
    $PRNFiles = Get-ChildItem -Path $Folder -Filter *.prn 
    $L = '^.L.*$'
    $zC = "zC"
    $Middle = "$L(.+?)$zC"
    
    Foreach ( $file in $PRNFiles ) {
        $Results = "C:\Scan_and_Print\$($file.basename + '_dpl').txt"
        Get-Content $file |
            Select-String -pattern $L, $Middle , $zC -AllMatches |
                Select -Expand Line |
                    Out-File $Results -Append
    }
    
  • #93222

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    Let's say you saved the content you showed up here in a file named "C:\sample\sample.txt" ... then you could try something like this:

    [INT]$Start = Select-String -Path C:\sample\sample.txt -Pattern '^L$' | Select-Object -ExpandProperty linenumber
    [INT]$End = Select-String -Path C:\sample\sample.txt -Pattern '^zC$' | Select-Object -ExpandProperty linenumber
    (Get-Content -Path C:\sample\sample.txt)[($Start -1 )..$End]

    Does this help you?

  • #93331

    Participant
    Points: 0
    Rank: Member

    Thank you so much. You got me on the right track, which after a few hours of playing around with it (remember, I'm a newbie), I found that this worked for what I need.

    $Folder = "c:\Scan_and_Print\"
    Get-ChildItem -Path $Folder -Filter *.prn | Foreach {
    New-Item -Path $Folder -Name "$( $_.BaseName + "_graphic" ).txt"
    New-Item -Path $Folder -Name "$( $_.BaseName + "_dpl" ).txt" }
    
    $PRNFiles = Get-ChildItem -Path $Folder -Filter *.prn 
    
    Foreach ( $file in $PRNFiles ) {
        $Results = "C:\Scan_and_Print\$($file.basename + '_dpl').txt"
        
        $zC = "zC"
        $Start = (Select-String -Path $file -Pattern '^.L.?$').LineNumber
        $End = (Select-String -Path $file -Pattern 'zC').LineNumber
        (Get-Content -Path $file)[($Start -1)..($End -1)] |
            Out-File $Results -Append
    }
    

    You have absolutely no idea how much time you have saved me!!!!! Thank you so much.

The topic ‘Select-String to return two lines (set to variables) and everything in between’ is closed to new replies.