![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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; |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
| Thread Tools | |
| Display Modes | |
|
|