[X-Unix] Remove last ten lines of files
Xavier Noria
fxn at hashref.com
Mon Jan 5 18:23:32 PST 2004
On Jan 5, 2004, at 9:32 PM, Victor Eijkhout wrote:
> 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
We'd need a definition of "last 10 lines":
fxn at conway:~/tmp% perl -e 'print "a\nb\n"' > bar.txt
fxn at conway:~/tmp% wc -l bar.txt
2 bar.txt
fxn at conway:~/tmp% perl -e 'print "a\nb"' > bar.txt
fxn at conway:~/tmp% wc -l bar.txt
1 bar.txt
Assuming the file ends in a newline and can be slurped, this would be a
filter in Perl:
$ perl -0777 -pe's/(.*\n){0,10}$//' filename
To modify the file in place instead:
$ perl -0777 -pi -e's/(.*\n){0,10}$//' filename
-- fxn
More information about the X-Unix
mailing list