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

data ------------------------- para1 foo bar para1 some stuff para2 other stuff para2 foo bar para3 this is enough ------------------------- if paragraph's lines start with para2, then append new line(s) to that paragraph desired ------------------------- para1 foo bar para1 some stuff para2 other stuff para2 foo bar para2 NEW STUFF para3 this is enough ------------------------- going nuts with trying to set -F to \n\n or using -00

Replies are listed 'Best First'.
Re: one liner append line(s) to matched paragraph
by hippo (Archbishop) on Aug 25, 2015 at 23:01 UTC
    perl -pe '$n = 1 if /^para2/; print "para2 NEW STUFF\n" if $n && /^$/;'

    seems to do the trick for me. HTH.

Re: one liner append line(s) to matched paragraph
by shmem (Chancellor) on Aug 26, 2015 at 06:32 UTC
    perl -p00 -le '$_.="\npara2 NEW STUFF" if /^para2/' filename
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: one liner append line(s) to matched paragraph
by 1nickt (Canon) on Aug 25, 2015 at 23:11 UTC

    Here's one way to do it with a regexp:

    # cat 1139933.dat para1 foo bar para1 some stuff para2 other stuff para2 foo bar para2 extra stuff para3 this is enough #

    # perl -Mstrict -wne' $/ = ""; s/^(para2.*)^$/$1new text\n/ms; print; ' 1139933.dat para1 foo bar para1 some stuff para2 other stuff para2 foo bar new text para2 extra stuff new text para3 this is enough #

    Update: new solution; hippo beat me to my first idea

    The way forward always starts with a minimal test.