#!/usr/bin/perl use strict; use warnings; our %rec; # formats need global variables, not mere lexicals format STDOUT_TOP = Product Product basic all overseas Release Type color colors shipping ------------------------------------------------------------------------------ . ; format STDOUT = ^>>>>>>>>>> ^<<<<<<<<<<< ^<<<<<<<<<< ^<<<<<<<<<<<<<<<<<< ^<<<<<<< ~~ $rec{rel}, $rec{type}, $rec{bascol}, $rec{allcol}, $rec{ovship} . ; # to read a file instead, use this and change "" below to "" # open IN, '<', $ARGV[0] or die "open $ARGV[0]: $!"; $: .= ','; # split filled lines also on comma while () { chomp; if (m/^Release Date([\d\/]+$)/) { %rec = (type => 'none', map { $_ => 'N/A' } qw(bascol allcol ovship)); $rec{rel} = $1; } $rec{type} = $1 if m/^\s+product [^(]+\(([^)]+)\)$/; $rec{bascol} = $1 if m/^\s+color basic (.*)$/; $rec{allcol} = $1 if m/^\s+color all (.*)$/; $rec{ovship} = $1 if m/^\s+overseas shipping (.*)$/; write if m/^$/; } write # emit the last record if there was no trailing blank line __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