Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

one liner append line(s) to matched paragraph

by PeteInPDX (Initiate)
on Aug 25, 2015 at 22:43 UTC ( [id://1139933]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139933]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2026-05-09 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.