CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [bha148] Match pattern and print the next line to the file: Edit Log



Chris Charley
User

Jan 4, 2013, 12:54 PM


Views: 1064
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";



Code
  #!/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:


Code
  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)


Edit Log:
Post edited by Chris Charley (User) on Jan 4, 2013, 12:56 PM
Post edited by Chris Charley (User) on Jan 4, 2013, 12:57 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives