locked
How get specifics lines RRS feed

  • Question

  • Select-String -Path 'C:\Users\jassb\Desktop\text.txt' -SimpleMatch "apple" | Select LineNumber, Line

    In the example below, print d678 and x444 (line -1 and line +3)

    g123
    d678
    apple
    i789
    bt897
    X444
    z767
    lemon
    g344
    d566

    Thank you for your help!

    Saturday, February 8, 2020 11:47 PM

All replies

  • You can't do it with a -SimpleMatch. You can do this, though:

    @"
    g123
    d678
    apple
    i789
    bt897
    X444
    z767
    lemon
    g344
    d566
    "@  -split "`n" | Select-String -Pattern "d678|x444" | select LineNumber,Line


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

    Sunday, February 9, 2020 12:03 AM
  • In this case a simple procedural solution would be easier to understand and use:

    $lines = Get-Content :\Users\jassb\Desktop\text.txt 
    for($i=0;$i -lt $lines.Count;$i++){
        if($lines[$i] -match 'apple'){
            $lines[$i-1]
            $lines[$i+3]
            break
        }
    }

    The PS declarative approach would be:

    $results = Select-String -Path 'C:\Users\jassb\Desktop\text.txt' -SimpleMatch apple -Context 3
    $results.Context.PreContext[-1]
    $results.Context.PostContext[3]
    


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Sunday, February 9, 2020 12:12 AM
    Sunday, February 9, 2020 12:05 AM
  • Wow you're fast.  ;-)
    @"
    g123
    d678
    apple
    i789
    bt897
    X444
    z767
    lemon
    g344
    d566
    "@ -split "`n" | Select-String -Pattern "apple" -Context 3 | Select-Object -ExpandProperty Context -OutVariable Context 
    
    $Context.Precontext[1]
    $Context.PostContext[2]


    Live long and prosper!

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


    • Edited by BOfH-666 Sunday, February 9, 2020 12:13 AM
    Sunday, February 9, 2020 12:12 AM
  • Faster than a speeding bullet? 😁


    \_(ツ)_/

    Sunday, February 9, 2020 1:24 AM
  • I liked this code!

    $lines = Get-Content :\Users\jassb\Desktop\text.txt 
    for($i=0;$i -lt $lines.Count;$i++){
        if($lines[$i] -match 'apple'){
            $lines[$i-1]
            $lines[$i+3]
            break
        }
    }

    I just need to put it in a loop, as there will be other occurrences.



    Sunday, February 9, 2020 3:06 AM
  • I just need to put it in a loop, as there will be other occurrences.

    So - put it in a loop. What's the problem?

    Live long and prosper!

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

    Sunday, February 9, 2020 11:23 AM
  • I'm having trouble changing the code to print to a file.

    $lines = Get-Content C:\Users\jassb\Desktop\FileText.txt 
    for($i=0;$i -lt $lines.Count;$i++){
    for($u=0;$u -lt $lines.Count;$u++){
        if($lines[$i] -match 'word'){
    	write-host ";"
            $lines[$i-1]
            $lines[$i+8]
    		$lines[$i+52]
    		$lines[$i+53]
    		$lines[$i+64]
    		$lines[$i+65]
    		$lines[$i+76]
    		$lines[$i+77]
    		$lines[$i+78]
           break
        }
    	break
    }
    }


    Sunday, February 9, 2020 2:40 PM
  • You may make a step back and explain as detailed as possible what it is what you're trying to accomplish. Please do not explain how you think you can solve this task explain what the task is.

    Live long and prosper!

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

    Sunday, February 9, 2020 3:02 PM
  • FileText.txt contain;

    "g123
    d678
    apple
    i789
    bt897
    X444
    z767
    lemon
    g344
    d566
    g543
    d669
    apple
    u780
    hu898
    f533
    g533
    r564
    apple
    teff
    yrff
    tefd
    g123
    h798
    apple
    i789
    bc887
    p454"

    The desired result is a file text contain:

    "d678
    X444
    d669
    f533
    r564
    tefd
    h798
    p454
    "

    Sunday, February 9, 2020 8:46 PM

  • The desired result is a file text contain:

    "d678
    X444
    d669
    f533
    r564
    tefd
    h798
    p454
    "

    So just output it to a file.

    You are asking us to write a script for you one line at a time.  In this forum you are required to write your own scripts.

    Please either learn PowerShell or hire someone to do this for you.


    \_(ツ)_/


    • Edited by jrv Sunday, February 9, 2020 9:42 PM
    Sunday, February 9, 2020 9:42 PM