in reply to process multiline text and print in desired format
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
|
|---|
| 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:13 UTC |