in reply to process multiline text and print in desired format
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: process multiline text and print in desired format
by ak_mmx (Novice) on Mar 18, 2021 at 01:21 UTC |