The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 05:14 AM
How to remove the lines from file using perl dipakg Shell Programming and Scripting 5 06-03-2008 04:49 AM
how do i strip this line using perl regex. ramky79 Shell Programming and Scripting 1 03-18-2008 08:10 AM
add lines in file with perl jinsh Shell Programming and Scripting 6 02-13-2008 06:50 AM
Strip 3 header lines and 4 trailer lines ganesh123 Shell Programming and Scripting 9 03-10-2007 01:15 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
strip first 4 and last 2 lines from a file using perl

Hi

I have a file from which i need to remove the first 4 and the last 2 lines.. i know how to do it with sed but i need to do it in a perl script.. can you please help me how to do that.

Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 294
You would use the Tie::File module. The Tie::File documentation should explain it.

Or you could read the file into an array and use an array slice to remove the lines:

open(FH,'file');
@array = <FH>;
close FH;
open(OUT,'>','file');
print OUT @array[4..$#array-2];
close OUT;
Reply With Quote
  #3 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
Thanks for the quick reply, i tried it but it dint work for me.. i can make my situation more clear... i need to copy the data from a file except for the first 4 and the last 2 lines to a new file .. say File1 has lines
a1
a2
a3
a4
a5
.
a59
a60
a61

my new file should contain
a5
..
a59

Hope im clear.. a quick solution is really appreciated... thanks
Reply With Quote
  #4 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 294
this should do eaxctly that:

open(FH,'file');
@array = <FH>;
close FH;
open(OUT,'>','outfile');
print OUT @array[4..$#array-2];
close OUT;

Might not work if the file is too big to fit into memory.
Reply With Quote
  #5 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
File size is 742B .. alrite i will try it one more time..
open(FH,'file'); #opens the file
@array = <FH>; # writes the file data into an array
close FH; #closes the file
open(OUT,'>','outfile'); #???? can u please tell me wats happening here
print OUT @array[4..$#array-2]; #???? and here toooo
close OUT; #?????

Thanks again for ur quick reply
Reply With Quote
  #6 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 294
1. open(OUT,'>','outfile'); #???? can u please tell me wats happening here
2. print OUT @array[4..$#array-2]; #???? and here toooo
3. close OUT; #?????

1. opens a new file for writing or opens an existing file for overwriting. Associates it with filehandle OUT.

2. prints the array minus the first 4 and last 2 elements to filehandle OUT, which in this case means the lines of the file. This is called an array slice: @array[n,n...n]. It would be the same as doing this:

shift @array;
shift @array;
shift @array
shift @array;
pop @array;
pop @array;

but the array slice will be much faster for what you are doing because it does not modify the array.

3. closes the file associated with IN filehandle.
Reply With Quote
  #7 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
That works... thanks a lot!! really appreciate it!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0