Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to parse out unwanted characters in a text file using Perl

by mizmaster22 (Novice)
on Jun 10, 2011 at 18:35 UTC ( [id://909184]=perlquestion: print w/replies, xml ) Need Help??

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

I wrote a Perl script to log into my telnet server and send the output to a text file, but I am getting a ton of weird characters within each sentence of output that I want to parse\take out. All I want to do is read in the text file and remove the unwanted characters line by line to display the correct output.

EDIT: Here are the first lines from my text file,the word data indicates the information I want and as you can see it is wrapped in the characters that I want to remove:

data data dat data data7data data data da +ta data8 +  + +7 Page 1 +8[3;23 data data data: data data data data, +data data: data data data data data data data data data

HERE IS MY SCRIPT:(Which is working perfectly)

my ($hostname, $line, $password, $pop, $username); $hostname = "local_host"; $username = "name"; $password = "pass"; $file = "log_file"; $file1 = "dump_log"; use Net::Telnet (); $pop = new Net::Telnet (Telnetmode => 1, Input_log => $file, dump_log +=> $file1); $pop->open(Host => $hostname,Port => port#); $pop->waitfor('/login: $/i'); $pop->print($username); $pop->waitfor('/password: $/i'); $pop->print($password); $pop->print("terminal type"); $pop->waitfor('/Command: $/i'); $pop->print("terminal_command"); $line = $pop->get; print $line; open FILE, "data.txt" or die $!; print FILE $line; close FILE;

Replies are listed 'Best First'.
Re: How to parse out unwanted characters in a text file using Perl
by ikegami (Patriarch) on Jun 10, 2011 at 18:36 UTC
    You seem to have forgotten to ask a question.
Re: How to parse out unwanted characters in a text file using Perl
by Not_a_Number (Prior) on Jun 10, 2011 at 19:26 UTC
    $str =~ s/[^data,]//g;

    or perhaps you have some other definition of 'unwanted characters'?

Re: How to parse out unwanted characters in a text file using Perl
by ikegami (Patriarch) on Jun 10, 2011 at 19:47 UTC
    Can you set the TERM=dumb in the remote session? That should avoid having those created in the first place.
      Can I set that from command line or within putty?

        putty? I thought you were using Net::Telnet?

        If you get a "normal" shell on the remote end, $pop->print("export TERM=dumb");.

        What's $pop->print("terminal type");?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://909184]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-26 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found