Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing all lines of a file except the first and last line? 3

Status
Not open for further replies.

nook6

Programmer
Dec 18, 2002
24
0
0
GB
hi if i have a file which has lets say 30 lines ( this is different for every file)

Is there a way i can read every line and then print out all lines except the first and last?

Nook6
 
simple: The trick is to defer printing till next time in the loop.


open(OUT,">$myoutfile") or die "cannot open $myoutfile\n";
open(IN,"$myfile") or die "cannot open $myfile\n";
my $linecounter=0;my $lineread;
while(<IN>){
if($linecounter){print OUT &quot;$lineread&quot;;$lineread=$_;}
$linecounter++;}
close(IN); close(OUT);

The first time linecounter=0, so it will just increment linecounter.
The second time, linecounter=1, but lineread is the (undefined) null string(no newline). So the code prints to OUT the null string. The next time through to this line
the 2nd line is appended
The last line is never printed(stored in lineread, but not printed)


This is elegant, but if you want something simpler
and do not have BIG files(that do not fit in your RAM)
you can do this
my @lines=<IN>;
shift @lines; #get rid of first line;
pop @lines; #get rid of last line
foreach my $line(@lines){#NOW print
print OUT &quot;$line\n&quot;;}

Hope this helps
svar



print $lineread if $linecounter; #takes care of 1st line
$linecounter++;$lineread=$_;}


next if $linecounter==1;

 
Load up a regular array, $recs , then print via :

for ($x=1; $x < $#recs; $x++)
{
print &quot;$recs[$x]\n&quot;;
}

$x=1 ........means get 2nd row
$x < $#recs ......will not print the last record
HTH
Dickie Bird (:)-)))
 
Yet another flavor, TMTOWTDI ;-)

Code:
#!/usr/bin/perl
open IPF, &quot;file_name&quot;;
@lines = <IPF>;
shift(@lines);
pop(@lines);
print @lines;

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I don't really like the memory intesive method of reading the WHOLE file into an array. Try this:
Code:
open(FILE, '/path');
<FILE>; # toss first line

while(<FILE>){
 print $l;
 $l = $_;
}

Just uses some simple logic to do what's needed.

--jim

* * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Nook6 - Go with Codiferous' splendidly succinct code ! Dickie Bird (:)-)))
 
Curious, what does it print the first time through the loop? Does print know that $l is undefined and prints nothing, or is undefined interpreted as the empty string, which is essentially the same for printing? ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks for the complement Dickie Bird.

icrf: Your understanding is correct. However, you made me realize that my code could execute in an unintended manner, becuase I'm assuming that $l is not previously defined in the code. A better way to do this would've been:
Code:
open(FILE, '/path');
<FILE>; # toss first line
{
 my $l;
 while(<FILE>){
  print $l;
  $l = $_;
 }
}
Using &quot;my&quot; inside of an anonymous block solves that problem. Using blocks like this are a neat way to solve scoping problems without rediculous code-contortions.

--jim

* * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top