Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit paragraphs with extra whitespace between them using Perl's paragraph mode
    text
    copied!<p>I'm trying to parse the output of <a href="http://linux.die.net/man/8/pvdisplay" rel="nofollow">pvdisplay(8)</a>, which prints a separate "paragraph" for each physical volume:</p> <pre><code> --- Physical volume --- PV Name /dev/sdb VG Name vg_virtual_01 PV Size 16.37 TiB / not usable 2.25 MiB Allocatable yes PE Size 4.00 MiB Total PE 4291533 Free PE 3830989 Allocated PE 460544 PV UUID zqi1Q6-tIag-ghQy-MmdJ-kyOS-XkmY-HpyQ51 --- Physical volume --- PV Name /dev/sda VG Name vg_virtual_02 PV Size 16.37 TiB / not usable 2.25 MiB Allocatable yes PE Size 4.00 MiB Total PE 4291533 Free PE 1525709 Allocated PE 2765824 PV UUID BlYvJW-ieVx-AjRg-p3r6-e4oC-TkPc-u2lf7x </code></pre> <p>Simple enough, right?</p> <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl use strict; use warnings; my @pvs = do { local $/ = ''; `pvdisplay`; }; </code></pre> <p>As it turns out, not as simple as I thought. After half an hour of bashing head on keyboard because my array was only getting a single item, I realized that the blank lines between paragraphs actually contain additional whitespace before the EOL. According to perlvar, <code>$/</code> treats empty lines as a terminator if set to the null string, with the caveat that empty lines cannot contain any spaces or tabs. And of course, <code>$/</code> is a string, not a regex, so I can't set it to something like <code>/^\s+$/</code>.</p> <p>The only way I can see around this is to first save the output to a file, strip the extraneous whitespace, and read it back in, but I would hate to create a temporary file for something so simple. Is there a more elegant way to do this?</p> <p><strong>Edit: I can do this using split(/^\s+$/m), but I'm just curious if there's a way to do it by changing $/</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload