473,387 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 13319


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
15
by: batman | last post by:
i have a text file that is like: date = OCT0606 asdf sdaf asdfasdgsdgh asdfsdfasdg START-OF-DATA asdfasdfg asdfgdfgsfg
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
1
by: sivadhanekula | last post by:
Hi everyone, I have a problem with my Mysql data. I have 2,95,67,456 lines of data which is too much and if I run this in MYSQL front-end it is telling "OUT OF MEMORY". Any way with my collegues...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.