Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://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,

Maneesh


In reply to matching first paragraph satisfying condition by mnshptl32

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found