470,648 Members | 1,587 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

Delete starting and ending lines of a file

Hi,
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..

TIA

Regards,
Seenu.
Jul 19 '05 #1
7 13136


"Srinivasa T.N." wrote:

Hi,
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..

TIA

Regards,
Seenu.

tail +4 <filename> | head -3
Jul 19 '05 #2
Srinivasa T.N. wrote:
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it?
This Question is Asked Frequently. Please see "perldoc -q delete":
"How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?"
I want to repeat on multiple files..


Please see "perldoc File::Find".

jue
Jul 19 '05 #3
se*****@cdotb.ernet.in (Srinivasa T.N.) wrote in message

I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..


sed -e '
1,4{
4!d
$!N;$!N
}
$d
N;P;D
' yourfile
Jul 19 '05 #4
In article <b5**************************@posting.google.com >,
Srinivasa T.N. <se*****@cdotb.ernet.in> wrote:
Hi,
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..

TIA

Regards,
Seenu.


tail +4 infile | tac | tail +4 | tac

assuming that tac is available.
Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de***@theworld.com | \___/ | http://world.std.com/~cpd
Jul 19 '05 #5

"Srinivasa T.N." <se*****@cdotb.ernet.in> wrote in message
news:b5**************************@posting.google.c om...
Hi,
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..

TIA

Regards,
Seenu.


I'm obviously not as experienced as the other people that have responded,
and didn't quite understand their methods so thought maybe you may not
(though you may - I dunno where everyone's at, I'm new to the newsgroup), so
I thought I'd share my method.

#begin code

open (FIL, "yourfile");
@array=<FIL>; #read the file into an array
close (FIL);
$end=@array; #total in array

$end=$end-3; #to delete the last lines
$i=3; #and the first

open (FIL, ">yourfile"); #reopen file for writing

while($i<$end){
print FIL "$array[$i]\n";
}
close (FIL);

#end code

I haven't tested it, and it may very well have errors in syntax (or other
functionality related items) but that should do what you want and not
require any extra libraries
Jul 19 '05 #6
Nick Santos wrote:
"Srinivasa T.N." <se*****@cdotb.ernet.in> wrote in message
news:b5**************************@posting.google.c om...
I want to delete the first 3 lines and last 3 lines of a file.
How can I do it? I want to repeat on multiple files..
open (FIL, "yourfile");
@array=<FIL>; #read the file into an array
close (FIL);
$end=@array; #total in array

$end=$end-3; #to delete the last lines
$i=3; #and the first

open (FIL, ">yourfile"); #reopen file for writing

while($i<$end){
print FIL "$array[$i]\n";
}


You change neither $i nor $end, so your loop will never terminate. Probably
you just forgot a
$i++;
somewhere.

But you can replace this whole while loop with a simple array slice
print FIL @array[3..$#array-3];
Then you don't even need those auxilliary variables $end or $i.
Please be careful, I didn't test it either and it may have a one-off error
for the upper bound.
And of course it may have unexpected results if the array contains less then
6 elements.
close (FIL);


jue
Jul 19 '05 #7
You definatly want to do it using tail +4 <file> | head -3

The tail command prints the last few lines of the file. A tail -10 prints
the last ten lines of a file. A tail +4 prints all but the last three lines
of the file. Much the same, head -3 prints everything from the fourth line
on. The | is a pipe which passes the information from the first part of the
command to the second.
"Nick Santos" <DS*@comcast.net> wrote in message
news:efVxb.250784$ao4.892339@attbi_s51...

"Srinivasa T.N." <se*****@cdotb.ernet.in> wrote in message
news:b5**************************@posting.google.c om...
Hi,
I want to delete the first 3 lines and last 3 lines of a file. How
can I do it? I want to repeat on multiple files..

TIA

Regards,
Seenu.
I'm obviously not as experienced as the other people that have responded,
and didn't quite understand their methods so thought maybe you may not
(though you may - I dunno where everyone's at, I'm new to the newsgroup),

so I thought I'd share my method.

#begin code

open (FIL, "yourfile");
@array=<FIL>; #read the file into an array
close (FIL);
$end=@array; #total in array

$end=$end-3; #to delete the last lines
$i=3; #and the first

open (FIL, ">yourfile"); #reopen file for writing

while($i<$end){
print FIL "$array[$i]\n";
}
close (FIL);

#end code

I haven't tested it, and it may very well have errors in syntax (or other
functionality related items) but that should do what you want and not
require any extra libraries

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Cylix | last post: by
12 posts views Thread by yufufi | last post: by
2 posts views Thread by Francesco Pietra | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.