LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2006, 04:00 PM   #1
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Rep: Reputation: 49
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
 
Old 04-21-2006, 09:38 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Code:
@fields = split /\s+/, $line, 5;
print "Username:  $fields[3]\n";
print "IP Block:  $fields[2]\n";
print "More Data: $fields[4]\n";
 
Old 04-21-2006, 09:53 PM   #3
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Original Poster
Rep: Reputation: 49
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!
 
Old 04-21-2006, 10:54 PM   #4
JrLz
Member
 
Registered: Mar 2004
Location: Jakarta
Posts: 164

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl script..please help. apenguinlinux Debian 1 08-05-2005 02:35 PM
Perl Script Help Rv5 Programming 5 10-03-2004 10:04 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 03:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration