Jeff Westman wrote:
Hello Fellow Perlites,

Hello,

I'm having difficulty parsing a file and could use some help here.
I've attached my code, output file, and sample job run.

Basically, I want to read an Oracle TNS file (tnsnames.ora) and list
the host name and service on that server based on this input file.  A
sample TNS entry is multi-lined and looke like this

[snip]

The script generally works fine, but it has a problem when I do not
have a blank line in between these entries.  In other words, if my
input file has

[snip]

(along with the others I am parsing).  Again, if I separate the two
with a blank line, it works fine.  But since this is a DBA-owned file,
I cannot do this.

[snip]

Any help would be appreciated.  I've been at this off and on for
several days.  Note, this is not a "production" file, but just a
utility script.

Thank you for providing a working program and data to test it with.

It looks like you don't really need to use paragraph mode, this should do what you want:

#!/usr/bin/perl
#
# tnsnames.pl -- reads tnsnames.ora, sorts by host name

use warnings;
use strict;

my $tnsFile = 'tnsnames.ora';

open F, '<', $tnsFile or die "Could not open file $tnsFile: $!\n";

my ( $host, $name, %tns ) = ( '', '' );

while ( <F> ) {

    $host = $1 if /\(HOST\s*=\s*(\w+)\)/;
    $name = $1 if /\(SERVICE_NAME\s*=\s*(\w+)\)/;

    if ( length $host and length $name ) {

        print "*** RESULT : host= $host, service= $name\n";

        $tns{ $host } = $name;
        ( $host, $name ) = ( '', '' );
        }
    }

close F;

for ( sort keys %tns ) {
    printf "%-8s = %s\n", $_, $tns{ $_ };
    }

__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to