perl - file reading - last line not displayed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers perl - file reading - last line not displayed
# 1  
Old 12-15-2006
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 Smilie
# 2  
Old 12-15-2006
Would you provide a test file and the expected output?
# 3  
Old 12-15-2006
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  
Old 12-15-2006
Code:
if( $i <= 6 )

# 5  
Old 12-16-2006
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?
# 6  
Old 12-18-2006
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
# 7  
Old 12-18-2006
Quote:
Originally Posted by cbkihong
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?
Thanks for the reply.

What am trying to do is, buffer some 'n' lines, do some processing ( probably here its display) and then continue till the end.
Code:
$ch = ();

#am trying to empty the array and fill it afresh with the new set of contents from the file.

Could you please explain whats happening with.
Code:
$ch = ($content);

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

3. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

4. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

5. Shell Programming and Scripting

Reading the file line by line in Perl

Hello Everyone, I have written a perl script that will load the entire data file into an array and then I would check the value of the specific column and then if interested I will write to a good file else I will write it to a bad file. But here, the problem is that if the data file is a... (15 Replies)
Discussion started by: filter
15 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

perl - output not being displayed

Hi All I have the following code sub pall_nvrm { my $cmd = `pall -w "/u/ab/scripts/dev/nvrmerros/nvrmpaller"`; print $cmd; } if i run pall -w "/u/ab/scripts/dev/nvrmerros/nvrmpaller" in a shell i get this sort of out put eagley: boxted: cadle: eabost: hales: (3 Replies)
Discussion started by: ab52
3 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Reading each line of a file in perl script

HI I need to read each line (test.txt) and store it in a array (@test) How to do it in perl. Suppose i have a file test.txt. I have to read each line of the test.txt file and store it in a array @test. How to do it in perl. Regards Harikrishna (3 Replies)
Discussion started by: Harikrishna
3 Replies
Login or Register to Ask a Question