By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Delete starting and ending lines of a file

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a


"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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.