LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-24-2010, 09:35 AM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
Perl extract information for a particular column


Hi,

I have a file which has the output as shown below:

Code:
Teams                             |  matches |Goals     |   YC     |   RC   
------------------------------------------------------------------------------
Liverpool:                        |          |          |          |          
Gerrard             		  |    97    |  100  	|  41665   |   1342   
Torres            		  |    75    |   90   	|   1929   |   630    
Babel            		  |    86    |   80   	|  10621   |   1666   
carregher   			  |    93    |   40   	|   1901   |   146    
                                  |          |          |          |    
     
Chelsea:                          |          |          |          |          
Lampard                           |    40    |   459    |   172    |   264    
Terry                      	  |    22    |   459    |    96    |   341    
Cole                     	  |    52    |   459    |   223    |   213    
                                  |          |          |          |    
     
Manutd:                           |          |          |          |          
Roooney                		  |    85    |    59    |    48    |   8.6    
Berba               		  |    44    |    67    |    28    |    36    
Giggs               		  |    35    |   130    |    43    |    81    
Scholes                  	  |    33    |   917    |   281    |   591
I need to extract the Info from the RC column for the first 4 players of liverpool.

The test code i have does the same,but can anyone show me a better way of doing it.I could do it easily with gawk -F"|" and print the respective column,but i need to do this in perl.. Its just a test code and the naming convention sucks. Thanks a bunch


Code:
#!/usr/bin/perl
use strict;
use warnings;
open (FH,'<','/local/a/tmp/disk.log') ;
my @x=<FH>;
my $i=1;
my @arr;
my @newarr;

# Reading the lines,where the 4 pool players occur.
foreach (@x) {
    $i++;
    next if ($i ~~ [1..4]); 
    push (@arr,$_) if ($i<='8'); 
}


my $count=0;
while ($arr[$count]) {
my @lol=split(/ /,$arr[$count]);
push(@newarr,(join("","$lol[-4]","$lol[-5]")));
$count++
}
Output:
Code:
1342
630
1666
146

Last edited by kdelover; 10-26-2010 at 03:42 AM.
 
Old 10-24-2010, 10:52 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
perl -F"\|" -ane '$f=4 and next if /Liverpool/;print $F[-1] if ($f-- && $f>=0)' mysoccerfile
 
1 members found this post helpful.
Old 10-24-2010, 10:59 AM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
AWESOME! Thanks ghostdog.i really need to know how this one liner works .I actually need to do this in a script without a oneliner. How do i get this perl -F"|" in a script?Only thing i think of split function.
 
Old 10-25-2010, 06:47 AM   #4
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
any help
 
Old 10-25-2010, 07:31 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
I am confused a little ... it appears your code is already outputting the correct information. Could it be improved .. probably, but you have the groundwork.

Does it look as succinct as ghostdog's, no but that comes with practice and time.
 
Old 10-25-2010, 07:59 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by kdelover View Post
How do i get this perl -F"|" in a script?Only thing i think of split function.
-F is basically just split, so yeah, just use split
 
Old 10-26-2010, 03:40 AM   #7
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Thanks grail and ghostdog. I wanted a solution based on regex,i'll see if i can do it that way. Thanks again
 
Old 10-26-2010, 04:24 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Quote:
I wanted a solution based on regex
Seems to me this was the first mention of this being a requirement?

If you still need help on it you may need to be more specific on what you want to regex?
 
  


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
[SOLVED] Write a script to extract information from db tables LadyAnne Linux - Newbie 3 05-25-2010 06:02 AM
[Perl] Sort a file by column Kunsheng Programming 4 04-24-2009 10:09 AM
Extract Local User Information borisys Red Hat 1 02-20-2009 11:53 AM
Bash: Extract Title Information From Playlist xxfunkxx Linux - Newbie 5 07-28-2004 03:31 AM
Extract information from mail boxes pcwulf Linux - Software 0 06-20-2004 09:04 PM


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

Main Menu
Advertisement
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