Tuesday, March 4, 2008

More Head

The last post lamented the lack of an easy way to get "all but the last N lines" of a file without programming.

Here, just in case you need it, is such a program
$ cat ~/bin/allbut
#!/usr/bin/perl -ws
use strict;

die "usage: $0 [-h] [-n=N] [filename]\n" if our $h;

our $n ||= 10; # how many on the end to omit; default=10;

@_ = (undef) x $n;

while (<>) {
push @_, $_ ;
$_ = shift @_;
print $_ if defined $_;
}
$ for i in {1..20}; do echo $i; done | allbut -n=13
1
2
3
4
5
6
7
Like head and tail, allbut defaults to 10, so allbut foo prints all but the last 10 lines of foo.

I was tempted to call it callipygian; however, that would be "all butt," and risk confusion with tail.

No comments: