strip first 4 and last 2 lines from a file using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting strip first 4 and last 2 lines from a file using perl
# 1  
Old 02-01-2008
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
# 2  
Old 02-01-2008
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;
# 3  
Old 02-01-2008
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
# 4  
Old 02-01-2008
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.
# 5  
Old 02-01-2008
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
# 6  
Old 02-01-2008
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.
# 7  
Old 02-01-2008
That works... thanks a lot!! really appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete lines from a file in PERL?

Hi, I have 500 MB of file. I want to retain first line and last line of the file. I am unaware of deleting lines from a file in PERL. How can i do it in PERL? Regards VANITHA (3 Replies)
Discussion started by: vanitham
3 Replies

2. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

3. Shell Programming and Scripting

Trying to strip some character in perl

I have some character strings and I would like to know how can I remove them using perl. My file contains the following output shown below. An example of a string in the file: + netmck(uid=500)-root + netmck(uid=500)-root + netmck(uid=500)-root + netmck(uid=0)-root . . . I would... (1 Reply)
Discussion started by: smlushing
1 Replies

4. Shell Programming and Scripting

How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character. Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)? Open file StreamedOutput.txt Search for ISA and delete the data up to the tilde ~ char... (7 Replies)
Discussion started by: sapedi
7 Replies

5. Shell Programming and Scripting

Strip one line from 2 blank lines in a file

Hi Is there any command to scan thru a file looking for 2 consecutive blank lines and if any remove one of them. Please let me know. Regards, Tipsy (6 Replies)
Discussion started by: tipsy
6 Replies

6. Shell Programming and Scripting

How to remove the lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some seed information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. here is the contents of .txt... (5 Replies)
Discussion started by: dipakg
5 Replies

7. Shell Programming and Scripting

how do i strip this line using perl regex.

I have a variable dynamically generated $batch = /dataload/R3P/interface/Bowne/reports/RDI00244.rpt Now I'd like to strip '/dataload/R3P/interface/Bowne/reports/RDI' and '.rpt' from this variable my output should be only 00244 how to do this using perl regex.I'm a newbie to perl and would... (1 Reply)
Discussion started by: ramky79
1 Replies

8. Shell Programming and Scripting

add lines in file with perl

How to search string like: a and replace to a a a : : a in a file with perl? Thanks, Grace (6 Replies)
Discussion started by: jinsh
6 Replies

9. Shell Programming and Scripting

Strip 3 header lines and 4 trailer lines

Hello friends, I want to remove 3 header lines and 4 trailer lines, I am using following , is it correct ? sed '1,3d';'4,$ d' filename (9 Replies)
Discussion started by: ganesh123
9 Replies
Login or Register to Ask a Question