The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading each line of a file in perl script Harikrishna Shell Programming and Scripting 3 05-29-2008 09:28 AM
reading text file line by line MizzGail Shell Programming and Scripting 6 04-14-2008 07:58 AM
Line by line file reading... and more! ProFiction Shell Programming and Scripting 6 07-26-2007 12:32 PM
Perl Reading from File karyn1617 Shell Programming and Scripting 1 02-17-2005 08:44 PM
Reading line by line from file. akpopa UNIX for Dummies Questions & Answers 4 08-30-2001 11:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-15-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
perl - file reading - last line not displayed

Hi,

Here is something that am trying with perl

Code:
#! /opt/third-party/bin/perl

open(fh, "s") || die "unable to open the file <small>";

@ch = ();
$i = 0;

while( $content = <fh> )
{
  if( $i <= 5 ) {
    push(@ch, $content);
    $i++;
  }
  else {
    $i = 1;
    foreach(@ch) {
      print "The file content is $_";
    }
    @ch = ();
  }
}

foreach(@ch) {
  print "The file content is $_";
}

exit 0

The last line is not displayed in any case, could you please provide some pointers for that?

Thanks
  #2 (permalink)  
Old 12-15-2006
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Would you provide a test file and the expected output?
  #3 (permalink)  
Old 12-15-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Thanks!

Here is what you have asked for


Code:
>cat s

abcd
adding
more
and then
wow is that working
abcd
last line


Code:
>>perl script.pl

The file content is abcd
The file content is adding
The file content is more
The file content is and then
The file content is wow is that working
The file content is abcd

You could apparently see that the last line from the file 's' is missing
  #4 (permalink)  
Old 12-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

Code:
if( $i <= 6 )

  #5 (permalink)  
Old 12-18-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Quote:
Originally Posted by anbu23
Code:
if( $i <= 6 )

I dont think this is the right way to do by altering the index counter value as the value suggested will not work for files of different sizes and different lines
  #6 (permalink)  
Old 12-18-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
In your script when control is within the else loop you are not assigning the line read from the file.
Thats why cbihong has added the line

Code:
$ch = ($content);

  #7 (permalink)  
Old 12-16-2006
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Do you mean this?


Code:

while( $content = <fh> )
{
  if( $i <= 5 ) {
    push(@ch, $content);
    $i++;
  }
  else {
    $i = 1;
    foreach(@ch) {
      print "The file content is $_";
    }
    @ch = ($content);
  }
}

What would you like to do actually with your program, as I don't really understand? It seems like you may wish to buffer 5 or 6 lines of a file before output, but why do that?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0