
Chris Charley
User
Jan 4, 2013, 12:54 PM
Views: 2415
|
Re: [bha148] Match pattern and print the next line to the file
|
|
|
If your data is consistent, with 'fixed widths', you might try unpack. If you want to prepare a format that is readable by Excel, then you could create a comma separated values file, (all fields separated by a comma). Your output line would look like: open OUT, '>', 'outfile.csv' or die "Could not write to the file"; #!/usr/bin/perl use strict; use warnings; my $site; my $got_headers; my $format = 'x18A15A15A14'; while (<DATA>) { if (/(INPUT-\d+)/) { $site = $1 } elsif (/SOFTWARE1/) { if (not $got_headers++) { print join(",", 'Site', unpack $format), "\n"; } print join(",", $site, unpack $format, <DATA>), "\n"; } } __DATA__ SITE RSITE COMB FHOP MODEL INPUT-0 1234 HYB SY G12 SOFTWARE1 SOFTWARE2 SOFTWARE3 TMODE B0705R031E B0705R031E TDM SITE RSITE COMB FHOP MODEL INPUT-1 1234 HYB SY G12 SOFTWARE1 SOFTWARE2 SOFTWARE3 TMODE B0705R031A B0705R031E B0705R031E IPM This prints: Site,SOFTWARE1,SOFTWARE2,SOFTWARE3 INPUT-0,,B0705R031E,B0705R031E INPUT-1,B0705R031A,B0705R031E,B0705R031E
(This post was edited by Chris Charley on Jan 4, 2013, 12:57 PM)
|