[comp.unix.questions] need help on unix

wangf@unixg.ubc.ca (Frank Wang) (06/11/91)

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.  

Thanks in advance.

tkevans@oss670.UUCP (Tim Evans) (06/11/91)

wangf@unixg.ubc.ca (Frank Wang) writes:

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

Deleting First Few Lines:

Use 'tail -nnn' (where 'nnn' is the number of lines in the file *minus* the
number of lines at the beginning you want to delete), with output re-
directed to another file.

Deleting Last Few Lines:

Use 'head' instead of tail

Bad News:

Unfortunately, not all UNIX's have both 'head' and 'tail'
-- 
INTERNET	tkevans@Soaf1.ssa.gov   PHONE:  (301) 965-3286
UUCP 		...!{rutgers|ames|uunet}!mimsy!woodb!tkevans
US MAIL		6401 Security Blvd, 2-Q-2 Operations, Baltimore, MD  21235	

dattier@vpnet.chi.il.us (David W. Tamkin) (06/11/91)

wangf@unixg.ubc.ca (Frank Wang) wrote in <1991Jun11.004109.21966@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@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@esd.sgi.com (reflector)    flamingo-request@mcs.com (digest)

gordon@osiris.cso.uiuc.edu (John Gordon) (06/12/91)

wangf@unixg.ubc.ca (Frank Wang) writes:

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

	Well, these may not be the most efficient ways, but they should
work:

	(Assuming we are working with "bigfile", which is 99999 lines long)

	% head -99989 bigfile > bigfile2

	(The above command makes a copy of bigfile, minus the last 10 lines)

	% tail +10 bigfile > bigfile3

	(The above command makes a copy of bigfile, minus the first 10 lines)


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

yzarn@lhdsy1.chevron.com (Philip Yzarn de Louraille) (06/13/91)

In article <1991Jun11.004109.21966@unixg.ubc.ca> wangf@unixg.ubc.ca (Frank Wang) writes:
>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.  
>

Find out how many lines you have "wc -l file"
Then create a file without the desired top or bottom lines
head -number file_name > new_file_name: to delete the bottom lines
tail -number file_name > new_file_name: to delete the top lines

where number is the number of lines you want to retain.
-- 
  Philip Yzarn de Louraille                 Internet: yzarn@chevron.com
  Research Support Division                 Unix & Open Systems
  Chevron Information & Technology Co.      Tel: (213) 694-9232
  P.O. Box 446, La Habra, CA 90633-0446     Fax: (213) 694-7709

guy@auspex.auspex.com (Guy Harris) (06/17/91)

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

In that case, beware of suggestions to use "tail" to delete the first
few lines, such as some suggestions others have made; "tail" buffers up
a *limited* amount of text, and if you have a 500,000 line file and you
want to delete the first 5 lines, the remaining 499,995 lines are quite
unlikely to fit in "tail"s buffer, given that the file is too large to
be fed to your editor.

"sed" can do a better job; if you want to remove the first N lines, try
"sed -n 'N-1,$p'", where "N-5" needs to be computed and literally
inserted in the command. 

dattier@vpnet.chi.il.us (David W. Tamkin) (06/18/91)

guy@auspex.auspex.com (Guy Harris) wrote in <8382@auspex.auspex.com>:

| "sed" can do a better job; if you want to remove the first N lines, try
| "sed -n 'N-1,$p'", where "N-5" needs to be computed and literally
| inserted in the command. 

If you want to remove the first N lines, you don't need to calculate N-1 nor
N-5.  You could calculate N+1 and write

sed -n 'N+1,$p'

or calculate nothing at all, do less typing, and write

sed '1,Nd'  # assuming N is an integer written in digits

Depending on your shell, if N is in a variable, you can use something like
(in sh or ksh)

sed "1,$N d"

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier@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@esd.sgi.com (reflector)    flamingo-request@mcs.com (digest)

ask@cbnews.cb.att.com (Arthur S. Kamlet) (06/26/91)

In article <8382@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>>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.  

Seems to me if the file is too large to be edited, then you want to
break it down into manageable sized files.    Just zapping the last
or first few lines may be OK for now, but would still require that
you edit a large file without any guarantee that your edit won't
take you over the limit.

So, if you have a split() available, try to split your file into
pieces, each of which is small enough.

When you want to put the pieces back together, UNIX supplies a
useful command, which is the inverse of split:      cat
-- 
Art Kamlet  a_s_kamlet@att.com  AT&T Bell Laboratories, Columbus