Heads up
February 16, 2009 at 11:58 PM by Dr. Drang
Tonight I was presented with more evidence that I’m getting old and losing (that’s loosing to you Internet youngsters) my memory.
I was looking for a Unix utility that would do the opposite of head
: print all but the first n
lines of a file1. tail
isn’t quite right. It will print the last n
lines of a file, so you might think that getting it to print all but the first few lines would be a simple matter of subtraction. But suppose you don’t know how many lines there are in the file? Suppose there isn’t a file at all, that the input is coming from a pipe? tail
won’t do in those situations.
I had a feeling that if Unix had such a utility it would be called behead
, so I typed beh
into Terminal and hit the Tab key. It completed to behead
! I wondered if it was a compiled program or some smart bit of shell scripting, so I typed which behead
, expecting to then cat
it to see whether it was a shell script or not.
The response to which
brought me up short. behead
was sitting in my own scripts directory, ~/bin
. I had apparently written it ages and ages ago and had the same oh-so-clever idea about what it should be called. I have no memory of doing this, nor do I remember needing such a utility in the past (although it’s easy to imagine such a need).
So anyway, here’s the behead
script in all its glory. It comes from back when I did all my scripting in Perl and didn’t feel the need to do any commenting.
1: #!/usr/bin/perl
2:
3: use Getopt::Std;
4:
5: getopts('n:', \%opts);
6: $omit = $opts{'n'} || 1;
7:
8: while(<>) {
9: print unless $. <= $omit;
10: }
The -n
option tells behead
how many lines to take off the top. If no option is given, it removes just the first line.
I’m pretty sure this is my own work and not something I copied. I’ve looked through the tables of contents of both editions of the Perl Cookbook and not seen an entry for this type of script. I’ve also Googled bits of the code and come up with nothing. But if I did copy it, and you know the original source, please let me know so I can give proper attribution. It’s a useful utility, no matter who wrote it.
-
I’m working on a script that sends HTML email and I wanted to see the HTML with the email header lines stripped off. ↩