Delete pair of lines in text file based on string in 2nd line

Answered Delete pair of lines in text file based on string in 2nd line

  • Friday, June 15, 2012 6:21 PM
     
     
    I'm attempting to write a Powershell script that cycles through a text file filled with pairs of lines.  There isn't any whitespace between these pairs of lines.  As I cycle through, I'm checking the 2nd line in each of these pairs for a certain string, and if it's a match, I want to delete that pair of lines.  This seems pretty simple, but I'm having difficulty getting the logic correct to do this.  What's the best way to do this?

All Replies

  • Friday, June 15, 2012 6:52 PM
     
     Answered Has Code

    Something like this?  (Not tested):

    $output = @()
    $pairs = Get-Content -Path 'inputfile.txt' -ReadCount 2
    foreach ($pair in $pairs) {
        if ($pair[1] -notmatch 'Pattern') {
            $output += $pair
            }
        }
    $output


    Grant Ward, a.k.a. Bigteddy

  • Friday, June 15, 2012 6:59 PM
     
     
    Thanks!  I figured it would be something simple.  I haven't tested yet, but I don't see any reason that shouldn't work.