Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: My first Perl script

by CukiMnstr (Deacon)
on Apr 10, 2003 at 00:42 UTC ( [id://249484]=note: print w/replies, xml ) Need Help??


in reply to My first Perl script

I don't know what you mean by "nice and neat"? Do you want only a part of the information from each line in firewall.txt? If so, then you probably want to check split(), or maybe want to use regular expressions. You will have to give us an example of "very ugly and not very clean" and then an example of "nice and neat" if you want more help...

I have a comment on your script as it is: you are reading all of firewall.txt into memory, which can bring problems if the file gets very large. It's better if you open it and then iterate through it searching the lines that interest you. It's also a good idea to check the return values of your open() calls:

open(FW_LOG, $firewall_log) or die "$firewall_log open() failed: $!"; open(MYNEWFILE, ">>$my_new_log") or die "$my_new_log open() failed: $!"; while ($line = <FW_LOG>) { if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block +ed/) { # your line transformations would go here... print MYNEWFILE $line; } } close(FW_LOG) or die "$firewall_log close() failed: $!"; close(MYNEWFILE) or die "$my_new_log close() failed: $!";

hope this helps,

update: added closing curly brace for if, removed colon at the end of filename $my_new_log

Replies are listed 'Best First'.
Re: Re: My first Perl script
by tarballed (Initiate) on Apr 10, 2003 at 19:54 UTC
    Ahh, I was able to modify my code with you what suggested. Here is the new code:

    #!/usr/bin/perl #Variables $firewall_log = "/home/jwilliams/firewall.txt"; $my_new_log = "/home/jwilliams/extracted_log"; open(FW_LOG, $firewall_log) or die "$firewall_log open() failed: $!"; open(MYNEWFILE, ">>$my_new_log:") or die "$my_new_log open() failed: $!"; while ($line = <FW_LOG>) { if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block +ed/) { # your line transformations would go here... print MYNEWFILE $line; } } close(FW_LOG); close(MYNEWFILE);
    Now, I just need to figure out how to clean up the extracted info.

    T.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found