need help on unix

David W. Tamkin dattier at vpnet.chi.il.us
Wed Jun 12 02:10:26 AEST 1991


wangf at unixg.ubc.ca (Frank Wang) wrote in <1991Jun11.004109.21966 at unixg.ubc.ca>:

| Does anyone out there know how to delete the first few lines or the last 
| few lines from a file without evoke a editor?  The reason is the file is
| too large to be edited.  

OK.  Let's see.  

Do you know precisely how many lines you want to drop?  If you want to get
rid of the first eight lines (for example),

tail +9 oldfile > newfile    or
sed '1,8d' oldfile > newfile

To drop the last, say, ten lines isn't so easy.  sed, for example (I'm sure
all this can be done more elegantly in awk or perl), doesn't know when the
last line is coming up until it receives EOF, so you cannot address line "$-10"
in sed.  If you can wc -l oldfile and see that it has, let us say, 50,000
lines, and you want to drop the last ten, then

sed '49990q' oldfile > newfile

will do it.  If you don't know the precise number of lines to drop but
are basing the point on the occurrence of an expression, the methods are
generally adaptable, and when they aren't you can use grep -n to get a
line number.

The key, I think, is to use some utility that examines a file as a stream
without trying to hold it all at once.

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier at vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591

"Parker Lewis Can't Lose" mailing list:
 flamingo-request at esd.sgi.com (reflector)    flamingo-request at mcs.com (digest)



More information about the Comp.unix.questions mailing list