Delete starting and ending lines of a file

S

Srinivasa T.N.

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.
 
G

Geezer From The Freezer

Srinivasa T.N. said:
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
 
J

Jürgen Exner

Srinivasa said:
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
 
R

rakesh sharma

(e-mail address removed) (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
 
C

Charles Demas

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
 
N

Nick Santos

Srinivasa T.N. said:
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
 
J

Jürgen Exner

Nick said:
Srinivasa T.N. said:
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
 
A

Amy G

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 said:
Srinivasa T.N. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top