Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hey guys, which is the best way to match a line in a text file and delete the line just below it ?

Replies are listed 'Best First'.
Re: match and then delete next line
by Juerd (Abbot) on Mar 11, 2002 at 20:52 UTC

    hey guys, which is the best way to match a line in a text file and delete the line just below it ?

    Short answer: hire a programmer.
    Long answer:

    perl -i -pe'$p && print; $p = /pattern/' a_text_file.txt

    Update Oops, Kanji was right - I wasn't quite answering the question :)
    Try this one instead:

    perl -i -ne'$p || print; $p = /pattern/' a_text_file.txt

    44696420796F7520732F2F2F65206F
    7220756E7061636B3F202F6D736720
    6D6521203A29202D2D204A75657264
    

      Doesn't that double print lines matching following /pattern/?

      Perhaps you meant something like ...

      perl -pi.bak -e '/^pattern$/ && <>' a_text_file.txt

      ...where the <> eats up the next line before it gets assigned to $_ and printed by the -p switch.

      Update: s/matching/following/;, but it took me a while to figure out why. Neat trick!++ :)

          --k.