LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Condensing Perl Script (https://www.linuxquestions.org/questions/programming-9/condensing-perl-script-437531/)

Poetics 04-21-2006 04:00 PM

Condensing Perl Script
 
I'm trying to extract various bits of data off a given line in a log file, and present them on multiple lines with labels. For instance, I want the following line to end up as presented below:

Code:

7226 12  64.142.0.156/30    dungis  thaxton-19.sa fast0/47 to michael2-1.sa

Username:  dungis
IP Block:  64.142.0.156/30
More Data:  thaxton-19.sa fast0/47 to michael2-1.sa

Every line in the target file is in the same format, so I don't need to worry about the bits of data being in different orders, but I have not yet come up with a good way to get the code concice enough for my tastes.

What I have so far:
Code:

$line =~ /[a-z][a-z0-9\-]+/;    #Our usernames can contain numbers and hyphens
$data = $';
print "\nUsername:  $&\n";
$line = /\d+\.\d+\.\d+\.\d+\/\d+/;    #Grep the IP
print "IP Block:  $&\n";
$data =~ s/\s+\b//;
print "More Data: $data\n";

Is there any way I can constrict that down? I'm fairly new to perl and am working on making my code not just more logical, but easier to modify and program for the future.

Thanks in advance,
-- Poetics

spooon 04-21-2006 09:38 PM

Code:

@fields = split /\s+/, $line, 5;
print "Username:  $fields[3]\n";
print "IP Block:  $fields[2]\n";
print "More Data: $fields[4]\n";


Poetics 04-21-2006 09:53 PM

I knew there existed a much easier way of doing that :) Looks like I need to work on my list operators more.

Thanks a lot, spooon!

JrLz 04-21-2006 10:54 PM

yeah, I'm a beginner too....this post excites me....
and looking to this thread(spooon's and Poetics' script),I realize that we can make things easier with Perl :D


All times are GMT -5. The time now is 05:54 PM.