The code is only going to do as much as you program it to do. You're not handling all the other fields at all.

I would treat each "\n\n" as a record separator so that you can easily distinguish between records. Alternatively any row that starts with "Release Date" could begin a new record. But your sample input makes it seem that double-newline is sufficient.

#!/usr/bin/env perl use strict; use warnings; local $/ = "\n\n"; # Set the input record separator to double-newline +so that each # grouping can be treated as a single record. output('Product Release', 'product Type', 'color basic', 'color all', +'overseas shipping'); while (my $record = <DATA>) { my ($date, $product, $color_basic, $color_all, $overseas, $shippin +g_charges, $warranty, $status); if ($record =~ m{^Release\s+Date(\d+/\d+/\d+)\n\s*product\s+(.+)$} +m) { ($date, $product) = ($1, $2); $color_basic = $record =~ m/^\s*color\s+basic\s+(.+)$/m + ? $1 : 'N/A'; $color_all = $record =~ m/^\s*color\s+all\s+(.+)$/m + ? $1 : 'N/A'; $overseas = $record =~ m/^\s*overseas\s+shipping\s+( +.+)$/m ? $1 : 'N/A'; $shipping_charges = $record =~ m/^\s*shipping\s+charges\s+(. ++)$/m ? $1 : 'N/A'; $warranty = $record =~ m/^\s*warranty\s+(.+)$/m + ? $1 : 'N/A'; $status = $record =~ m/^\s*(.+)\Z(?!.)/m + ? $1 : 'No status'; output("Release Date$date", $product, $color_basic, $color_all +, $overseas); } else { warn "Bad record: $record\n"; } } sub output { my @fields = @_; printf "%-24s%-32s%-24s%-24s%-24s\n", @fields; } __DATA__ Release Date2/2/2019 product clock1(analog) color basic white color all white,black,silver warranty 1 year not sold yet Release Date2/2/2020 product none Release Date2/2/2021 product clock1(digital) color basic black color all black,silver warranty 1 year not sold yet Release Date2/2/2022 product clock2(digital) color basic white color all white overseas shipping yes shipping charges yes warranty 1 year not sold yet

This produces:

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

Your sample output doesn't do anything with shipping charges, and with warranty, or with the inventory status. So although this sample captures them into variables, it's not getting printed in output.


Dave


In reply to Re: process multiline text and print in desired format by davido
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.