Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Greetings! I just registered here and hope this is an appropriate venue for my question.

I'm new to perl and am trying to write a perl one-liner that returns everything from the first non-indented line of a file up until the end of that paragraph, terminated by a blank line. I can do this using awk with the command:

awk -v RS='' -v ORS='\n\n' '/^[^ ].*$/' file.txt | awk -v RS='' 'NR==1 +{print $0}'
My problem translating this to perl is that the first non-indented line of the file may or may not be the first line of the file. In the former case, this works:
perl -0pe 's/.*?\n*?([^ \n].*?)\n\n.*/$1/gs' file.txt
as does this:
perl -0pe 's/([^ \n].*?)\n\n.*/$1/gs' file.txt
In the latter case, this works:
perl -0pe 's/.*?\n([^ \n].*?)\n\n.*/$1/gs' file.txt
But is there a simple perl one-liner that works in both cases? I've tried writing a semicolon-separated perl command intended to prepend a blank line to the file before the search in the event that the first line is not indented, using something like
if ( $. == 1 and /^[^ ].*$/ ) {...}
but I can't get the syntax right. Obviously I could string together a sequence of commands like
echo '' > tempfile.txt ; cat file.txt >> tempfile.txt ; perl ...
or use some bash conditional like
if [[ $(egrep -n -m1 -e '^[^ ]' file.txt | sed 's/^\([0-9]\+\):.*/\1/g +') -eq 1 ]] ; then perl ... ; else perl ... ; fi
however, I'd like to know if there's some more elegant "pure perl" solution I'm overlooking.

Best regards,


In reply to matching first paragraph satisfying condition by mnshptl32

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (3)
    As of 2020-01-24 01:21 GMT
    Find Nodes?
      Voting Booth?