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:
Match pattern and print the next line to the file

 



bha148
Novice

Jan 4, 2013, 9:47 AM

Post #1 of 6 (1286 views)
Match pattern and print the next line to the file Can't Post

Hi Guru's..
I am trying to do simple text processing script. Currently I am using this script. After matching the pattern, it writes the next line to the output file. It works fine and it give me the output, but I still need to process the output in excel to arrange the data's.

Code
#! /usr/bin/perl -w 
open INF, 'infile.txt' or die "Could not open file";
open OUT, '>outfile.txt' or die "Could not write to the file";

while (<INF>)
{
print OUT $_=<INF> if /SOFTWARE1/ ;
}

Here is the input...

Code
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

I would like to write to the output file with header and under each header respective values. Sometimes any one of the value can be blank space.

Code
Site     Software-1 Software-2 Software-3 
input-0 abc abc abc

Kindly suggest me some basic Perl methods to do this.
Thank you in advance.


7stud
Enthusiast

Jan 4, 2013, 10:53 AM

Post #2 of 6 (1280 views)
Re: [bha148] Match pattern and print the next line to the file [In reply to] Can't Post


Code
 
use strict;
use warnings;
use 5.012;

printf "%-20s %-10s %-20s", 'hello', 'world', 'goodbye';

--output:--
12345678901234567890123456789012345678901234567890
hello world goodbye



Chris Charley
User

Jan 4, 2013, 12:54 PM

Post #3 of 6 (1264 views)
Re: [bha148] Match pattern and print the next line to the file [In reply to] Can't Post

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)


bha148
Novice

Jan 4, 2013, 1:24 PM

Post #4 of 6 (1258 views)
Re: [Chris Charley] Match pattern and print the next line to the file [In reply to] Can't Post

Excellent... It is working well. I still need to understand some of the commands in this script. I will try to decode and understand.
Thank you very much.


Chris Charley
User

Jan 4, 2013, 5:55 PM

Post #5 of 6 (1250 views)
Re: [bha148] Match pattern and print the next line to the file [In reply to] Can't Post

Glad it is working for you.

Maybe I can explain some information on what the $format variable I am using for the unpack function.


Code
my $format = 'x18A15A15A14';


This tells unpack how to parse the text you want.

x18 - skip first 18 characters (which are 18 spaces that precede the first field you want to capture).
A15 - capture the next 15 characters and spaces
A15 - same thing
A14 - capture next 14 spaces or characters.

So that explains about the $format variable.

One behavior when unpacking text with 'A' is that it will trim off any trailing space.

So, in this example, when using A15 to capture the first field:


Code
A15 gets [SOFTWARE1      ] (with trailing spaces) 
and trims it to [SOFTWARE1] - trims trailing spaces.



bha148
Novice

Jan 4, 2013, 6:19 PM

Post #6 of 6 (1245 views)
Re: [Chris Charley] Match pattern and print the next line to the file [In reply to] Can't Post

Thanks mate, for your explanation. Appreciate that!. It really helpful for new learners like me..
Cheers!

 
 


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

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