G'day ak_mmx,

Welcome to the Monastery.

The following script shows how I'd probably attack this problem. See the notes at the end for some details.

#!/usr/bin/env perl use strict; use warnings; use autodie; my $data = 'pm_11129830_input.txt'; my $fmt = "%-24s%-15s%-14s%-24s%s\n"; my @headings = ( 'Product Release', 'product Type', 'color basic', 'color all', 'overseas shipping', ); printf $fmt, @headings; { open my $fh, '<', $data; local $/ = ''; while (<$fh>) { my ($rel, $type, $colb, $cola, $ship) = ('', 'none', 'N/A', 'N/A', 'none'); for my $item (map /^\s*(.+)$/, split /\n/) { for ($item) { /^Release Date/ && do { $rel = $item; last; }; /^product [^(]+\(([^()]+)/ && do { $type = $1; last; }; /^color basic (.+)$/ && do { $colb = $1; last; }; /^color all (.+)$/ && do { $cola = $1; last; }; /^shipping charges (.+)$/ && do { $ship = $1; last; }; } } printf $fmt, $rel, $type, $colb, $cola, $ship; } }

Output:

Product Release product Type color basic color all + overseas shipping Release Date2/2/2019 analog white white,black,silve +r none Release Date2/2/2020 none N/A N/A + none Release Date2/2/2021 digital black black,silver + none Release Date2/2/2022 digital white white + yes

Notes:

— Ken


In reply to Re: process multiline text and print in desired format by kcott
in thread process multiline text and print in desired format by ak_mmx

Title:
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, details, 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, summary, 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.