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: Intermediate:
CHALLENGING TEXT PATTERN II (CLONE WARS)

 



nanohurtz
Novice

Mar 25, 2002, 8:44 AM

Post #1 of 6 (730 views)
CHALLENGING TEXT PATTERN II (CLONE WARS) Can't Post

Hi,

I'm looking for a .pl code snippet that can read a .txt flat file of 12,000 jobs like the ones included below (FLATFILE INPUT) and output to 6 column .xls output as also included. Please note that some of the lines defy the $_=~/m^(.*)#(.*) or (SERVER)#(JOB) pattern because the flat file was generated from server SPBS565A. I need the first column of every instance of this occurance to be replaced with SPBS565A (See in red). Must be in .xls output because some SCRIPTNAME or DOCOMMANDS may have "|" delimiters in them..Any help is greatly appreciated.

FLATFILE INPUT (.txt)

MBQR123A#RPM123DP SCRIPTNAME "/usr/bin/tryout.sh"
STREAMLOGON "root"
DESCRIPTION "Experimental Script"
RECOVERY STOP


SDRF555A#RTD123DP DOCOMMAND "/usr/ben/hammerhead.sh"
STREAMLOGON "root"
DESCRIPTION "Some silly script"
RECOVERY STOP


GGG366DP SCRIPTNAME "/usr/local/sniffer.sh"
STREAMLOGON "fred"
DESCRIPTION "Checks for Ports"
RECOVERY STOP


UUU777RP DOCOMMAND "banner USA"
STREAMLOGON "samm"
DESCRIPTION "God bless America"
RECOVERY STOP


QWQW455A#CCX888DP SCRIPTNAME "/usr/bin/headache.sh"
STREAMLOGON "root"
DESCRIPTION "CPU Stablization"
RECOVERY STOP


SAMPLE FLATFILE OUTPUT (.xls)

MBQR123A | RPM123DP | /usr/bin/tryout.sh | root | Experimental Script | STOP
SDRF555A | RTD123DP | /usr/ben/hammerhead.sh | root | Some silly script | STOP
SPBS565A | GGG366DP | /usr/local/sniffer.sh | fred | Checks for Ports | STOP
SPBS565A | UUU777RP | banner USA | samm | God Bless America | STOP
QWQW455A | CCX888DP | /usr/bin/headache.sh | root | CPU Stablization | STOP


-Pete Crazy
-NanoHurtz

"The danger from computers is not that they will eventually get as smart as men, but
we will meanwhile agree to meet them halfway."
-Bernard Avishai


nanohurtz
Novice

Mar 25, 2002, 8:45 AM

Post #2 of 6 (729 views)
Re: [nanohurtz] CHALLENGING TEXT PATTERN II (CLONE WARS) [In reply to] Can't Post

My apologies for the caps in subjext ..I know its a no-no

-Pete Crazy
-NanoHurtz

"The danger from computers is not that they will eventually get as smart as men, but
we will meanwhile agree to meet them halfway."
-Bernard Avishai


mhx
Enthusiast / Moderator

Mar 26, 2002, 10:41 PM

Post #3 of 6 (714 views)
Re: [nanohurtz] CHALLENGING TEXT PATTERN II (CLONE WARS) [In reply to] Can't Post

[perl]
#!/usr/bin/perl -w
use strict;

my $infile = 'input.txt';
my $outfile = 'output.xls';

open IN, $infile or die "cannot open $infile: $!\n";

my @data = map { s/^\s*(?:(\w+)#)?(\w+)\s*//;
{ SERVER => $1 || 'SPBS565A', JOB => $2,
map { (/([A-Z]+)\s*(.*?)\s*$/) } split $/ }
} do { local $/; <IN> } =~ /.*?^RECOVERY.*?$/gms;

close IN;

open OUT, ">$outfile" or die "cannot open $outfile: $!\n";
for( @data ) {
print OUT join( ' | ', @$_{qw(SERVER JOB)},
$_->{SCRIPTNAME} || $_->{DOCOMMAND},
@$_{qw(STREAMLOGON DESCRIPTION RECOVERY)} ), "\n"
}
close OUT;
[/perl]

Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



nanohurtz
Novice

Mar 27, 2002, 9:38 AM

Post #4 of 6 (708 views)
Re: [nanohurtz] CHALLENGING TEXT PATTERN II (CLONE WARS) [In reply to] Can't Post

MHX, where's the explanation?.. Wow, and I thought Japanese was easy. No problem, I just need to study its syntacs more. Although the code checks out with fine (at least with OptiPerl), it doesn't produce an output, in either .xls or .txt. ..blank files.. What's missing? I'd like the output in .xls and I have a feeling the the ' | ' delimiter is not going to help..
-NanoHurtz

"The danger from computers is not that they will eventually get as smart as men, but
we will meanwhile agree to meet them halfway."
-Bernard Avishai


mhx
Enthusiast / Moderator

Mar 27, 2002, 10:48 AM

Post #5 of 6 (707 views)
Re: [nanohurtz] CHALLENGING TEXT PATTERN II (CLONE WARS) [In reply to] Can't Post


In Reply To
MHX, where's the explanation?


Um, well, I was a bit in a hurry when I posted this, so I skipped the explanation in the hope that it would be clear what I did... Ok, when I see the code now, I realize that it looks a bit funny...


In Reply To
Although the code checks out with fine (at least with OptiPerl), it doesn't produce an output, in either .xls or .txt. ..blank files.. What's missing? I'd like the output in .xls and I have a feeling the the ' | ' delimiter is not going to help..


I've tested it, it works absolutely fine with both, perl-5.6.0 under HP-UX and perl-5.6.1 under Windows (ActiveState). It produces a file named output.xls in the format you've specified. If you don't like the ' | ' delimiter (which is in your sample output), change it to anything else you need.

Now, for the explanation. The file opening and such stuff should be obvious, so I'll concentrate on the map and the for loop. The map extracts all information from the input file and is the only complex part of the script. I'll reformat it a little bit:

[perl]
my @data = map {
s/^\s*(?:(\w+)#)?(\w+)\s*//;
{
SERVER => $1 || 'SPBS565A',
JOB => $2,
map { (/([A-Z]+)\s*(.*?)\s*$/) } split $/
}
}
do { local $/; <IN> } =~ /.*?^RECOVERY.*?$/gms;
[/perl]

Starting in the last line, the do { ... } will read the whole input file (by locally undefining $/). The contents of the file will then be matched against a regular expression, which will return a list of all records of the input file. This is done with the /g modifier, which matches all occurences of the pattern and returns a list of the matched strings. The /m and /s modifiers change the behaviour of the . and the ^ and $ meta-characters.

Next, the map block is executed for each record of the input file. First, I search for the server/job pattern (stored in $1 and $2) and remove it from the string. After that, I create the hash reference that will be returned by the map block. The referenced hash is assiged entries that hold SERVER and JOB name, the server defaults to SPBS565A if it's not present. Also, it is assigned additional entries from the embedded map. The split $/ returns the list of input to the map, which is just the list of lines left in $_ (which is why I split at $/). Inside the map, I extract key and value for the hash entry using a regular expression.

Each referenced hash in the @data array now has keys like SERVER, JOB, DOCOMMAND, STREAMLOGON or RECOVERY, and the corresponding values. That's why the final for loop

[perl]
for( @data ) {
print OUT join( ' | ', @$_{qw(SERVER JOB)},
$_->{SCRIPTNAME} || $_->{DOCOMMAND},
@$_{qw(STREAMLOGON DESCRIPTION RECOVERY)} ), "\n"
}
[/perl]

turns out to be rather simple. The only unusual thing may be the use of hash slices, but that's very useful here. Since only one of SCRIPTNAME and DOCOMMAND is present, I use || to get whichever is present.

That's it, if you have any specific questions, feel free to ask.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



nanohurtz
Novice

Mar 27, 2002, 9:36 PM

Post #6 of 6 (703 views)
Re: [nanohurtz] CHALLENGING TEXT PATTERN II (CLONE WARS) [In reply to] Can't Post

Whew, I definitely have enough to keep me in the books for a while. Still trying to get this awesome code to work on either the my Win32 (Active State 5.6.1) or my Linux box. Thats ok though, nothing like a good challenge to heighten understanding. Thanks again for your help and your patience Mhx. Smile
-NanoHurtz

"The danger from computers is not that they will eventually get as smart as men, but
we will meanwhile agree to meet them halfway."
-Bernard Avishai

 
 


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

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