Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

one liner append line(s) to matched paragraph

by PeteInPDX (Initiate)
on Aug 25, 2015 at 22:43 UTC ( #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 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 hippo (Chancellor) 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 1nickt (Abbot) 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
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? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2020-10-26 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?