Deleting Lines above and below a line of text

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of random commandline random commandline 6 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36221
    Profile photo of Kyle Cribbs
    Kyle Cribbs
    Participant

    I decided to make a new thread and ask a much simpler (hopefully) question.

    Lets say i had a text file that contained

    mouse
    cat
    cat
    cat
    dog
    mouse
    dog
    dog
    mouse
    cat
    mouse

    and i wanted it to search for "dog" and once found i would want it to delete all the lines above dog until it see's cat.. as well as delete everything below dog until it see's cat as well..

    so the result would be

    mouse
    cat
    cat
    cat
    cat
    mouse

    #36222
    Profile photo of random commandline
    random commandline
    Participant

    Are you looking for all lines excluding 'dog'? If so, just use something like this.

    # Sample array
    $string = @" 
    mouse
    cat
    cat
    cat
    dog
    mouse
    dog
    dog
    mouse
    cat
    mouse
    "@ -split "`n"
    
    ($string -notmatch "dog").Trim() | Select-Object -Unique
    # Result
    # mouse
    # cat
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.