LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2011, 05:15 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
Perl FILEHANDLE chops off first line on READ


So i only need certain parts of this bash log - the time, the user and
the commands executed.

Code:
casperbox 12292011 09:12:49 bash: user: casper as casper from ip: 10.80.6.100:pts/55 execs: \'sudo grep closedata /data/*\'
casperbox 12292011 09:13:15 bash: user: casper as casper from ip: 10.80.6.100:pts/55 execs: \'cd ~/nocteam/group/\'
casperbox 12292011 09:13:19 bash: user: casper as casper from ip: 10.80.6.100:pts/55 execs: \'xcd /var/ftp/\'
casperbox 12292011 09:13:22 bash: user: casper as casper from ip: 10.80.6.100:pts/55 execs: 'cd /var/ftp/\'
I use this perl script - but it keeps on chopping off the first line.

Code:
#!/usr/bin/perl 
open (FILE, "/var/log/bashLog") or die ("Can not open the File $!"); 
while (<FILE>) { 
 @array = <FILE> ; 
 foreach $line (@array) {
 @split_array=split(" ", $line); 
 @splice =  @split_array[2,7,13..24] ;
 print "@splice\n";
 }
}

it cuts off the first line

these are the results:
Code:
09:12:49 casper grep closedata /data/*\'         
09:13:15 casper ~/nocteam/group/\'           
09:13:19 casper /var/ftp/\'
The first line of every file that gets read in to the FILEHANDLE is chopped off. There is something wrong with the file handle and the while loop.

Last edited by casperdaghost; 12-30-2011 at 05:56 PM.
 
Old 12-30-2011, 06:07 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Using both the while() loop and the @array file slurp creates superfluous (and harmful, as you discovered) code. Eliminate one or the other, a la:

Code:
#!/usr/bin/perl

use warnings ;
use strict ;

my $infile = 'bar.txt' ;

open my $FILE, '<', $infile or die "Unable to open $infile: ($!)" ;

while(<$FILE>) {

  my @line = split() ;
  print "@line[1,3,7]\n" ;

}
 
1 members found this post helpful.
Old 12-30-2011, 06:19 PM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by casperdaghost View Post
I use this perl script - but it keeps on chopping off the first line.

Code:
#!/usr/bin/perl 
open (FILE, "/var/log/bashLog") or die ("Can not open the File $!"); 
while (<FILE>) { 
 @array = <FILE> ; 
 foreach $line (@array) {
 ...
 }
}
You read the first line in while (<FILE>), but you never process it - you then read the rest of the file in @array = <FILE>. You shouldn't be doing both a while <FILE> and grabbing the whole array and looping over it.

eta: OK, exactly as anomie said.
 
1 members found this post helpful.
  


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
Trying to get Understanding on filehandle and split function in Perl jmc1987 Programming 13 11-29-2011 10:34 PM
[SOLVED] perl script to read line by line casualzone Programming 26 08-03-2011 01:27 PM
Perl filehandle problems DanTaylor Programming 8 01-27-2006 05:57 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 08:45 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration