[X-Unix] Remove last ten lines of files

David Ledger dledger at ivdcs.demon.co.uk
Tue Jan 6 14:07:39 PST 2004


>From: Victor Eijkhout <eijkhout at cs.utk.edu>
>At 21:45 +0900 2004/01/02, Kino wrote:
>>On Jan 2, 2004, at 8:04 PM, Kuestner, Bjoern wrote:
>>
>>>does anybody have a simple solution to remove the last ten lines of files
>>>(of variable length).
>>
>>#! /bin/sh
>>
>>i=`grep -c $ $*`
>>j=`expr $i - 10`
>>if [ $j -lt 1 ]; then
>>j=$i
>>fi
>>head -n $j $*
>
>   awk -v n=`tail -n +10 YOURFILE | wc -l` 'NR<n {print}' YOURFILE
>
>Tada!

	or even
ed YOURFILE
1,$-10p
q
	which can be done inline as
echo '1,$-10p;q' | tr ";" "\n" | ed YOURFILE
	(the version with embedded newlines not being likely to survive email)

David


-- 
David Ledger - Freelance Unix Sysadmin in the UK.
Chair of HPUX SysAdmin SIG of hpUG technical user group (www.hpug.org.uk)
dledger at ivdcs.co.uk (also dledger at ivdcs.demon.co.uk)
www.ivdcs.co.uk



More information about the X-Unix mailing list