Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
network1 12.435.23.12 12.45.34.23 network2 32.334.23.23 54.56.563.32
This is the text file i have. if it contains alphabets,perl should create a file name else it should login to the device with the given ip address in the file.
#/usr/bin/perl use Net::Telnet; use Net::Telnet::Cisco; #Opening a input text file for logging into devices open inFile, '<', 'ip.txt' or die "MESSAGE From SAT : Couldn't open ip +ut.txt for reading verify the correct path : $!"; print " hello \n\n "; #storing the contents of the input file to a variable print " Reading the file . . .\n\n"; while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; print $line,"\n\n"; if ($line=/.*?\w/) { $accountname = $1; print "Account name =", $accountname; open ("$accountname"), or die "Could not create the account name.txt.\ +n"; ................ . . } }
can anyone give suggestions on this ?

Replies are listed 'Best First'.
Re: text parsing
by shmem (Chancellor) on Jul 07, 2006 at 08:29 UTC

    You probably want to process each line in turn instead of concatenating all input together:

    while ($line = <inFile>) { chomp; if ($line =~ /^([\d\.]+)$/) { ... } }

    Read perlre.

    Your line if ($line=/.*?\w/) lacks the parens () in the regular expression to capture $1. Also, it matches any IP address, because each has a number which matches \w and .*? says "0 or more of any char, but the least necessary amount of them for the whole expression to match". Since you are not using ^ or $ the match can be ocurring anywhere, so it matches the first \w character in the line.

    Use /^([\d\.]+)$/ to catch an ip-address, and /^([a-z]+)$/i to catch only letters from a-z (case insensitive).

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: text parsing
by Moron (Curate) on Jul 07, 2006 at 11:04 UTC
    some hints

    - terminate argument to die with "\n" if you want to suppress the traceback information

    - while( my $line = <inFile> ) {

    - it is a good habit to always specify full path names rather than just filenames

    - you might want to try running with 'perl -d <scriptfile>' when debugging. The command 'h' will give a full set of debug commands available.

    Update: also recommendable:

    - declare all variables with 'my' and insert the following controls at the top of the program:

    use strict; use warnings;

    -M

    Free your mind

      Hi Thank you for the very useful tips. i would love to show/explain the algorithm to you.
      1.open a text file 2.if the text file contains alphabets then create a (test1.txt) file w +ith the name and telnet to the devices(34.56.67.78) mentioned under t +he test1 and execute the commands and store command output in the sam +e test1.txt file and telnet to device2(65.45.76.343)and run the same +commands.and if found next text (peak) then create peak.txt and so on... test1 34.56.67.78 65.45.76.343 peak 34.56.56.45 23.65.766.76 sdsd 45.65.76.56 343.45.45.453 435.65.67.878
        To get alphabetics, use the POSIX IsAlpha class, also it is usually easier and more maintainable to make loops into functional (based on key logic) rather than technical blocks (e.g. based on fh open state instead), for example:
        use strict; use warnings; use ... etc.; my $filename = 'filename'; my $msgfront = 'MESSAGE FROM SAT: '; my $path = $ENV{ PATHNAME } . "/$filename"; open my $ifh "<$path" or die $msgfront . "$!: $filename\n"; # 'group' loop for ( $_ = <$ifh>; $_ = <$ifh> and /:IsAlpha:/; ) { chop; my ( $group, @address ) = ( $_, () ); # 'address' loop for ( $_ = <$ifh> ; $_ = <$ifh> and !/:IsAlpha:/ ) { chop and push @address, $_; } # process $group with its @address -es here } close $ifh;

        -M

        Free your mind

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.