Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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:

7226 12     dungis fast0/47 to

Username:   dungis
IP Block:
More Data: fast0/47 to
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:
$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, 10:38 PM   #2
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
@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, 10:53 PM   #3
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

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, 11:54 PM   #4
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


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 03:35 PM
Perl Script Help Rv5 Programming 5 10-03-2004 11:04 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 07:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

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