#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'skip' is the missing complement to 'head'. Moreover, it can also redirect
# the skipped lines to a file.
#
# $Id: skip,v 1.4 2007/12/18 03:06:17 bojar Exp $
#

use strict;
use Getopt::Long;

my $usage = 0;
my $save = undef;
GetOptions(
  "save=s" => \$save,
  "help" => \$usage,
) or exit 1;

my $num = shift;

if (! defined $num || $usage) {
  print STDERR "skip <number_of_lines>
Skips the specified number of lines and 'cat's the rest.
Options:
  --save=filename  ... save the skipped lines here
";
  exit 1;
}

if (defined $save) {
  open SAVE, ">$save" or die "Can't write $save";
}
my $cnt = 0;
while (<>) {
  $cnt ++;
  if ($cnt <= $num) {
    print SAVE $_ if defined $save;
    next;
  }
  print;
  if (defined $save) {
    close SAVE;
    $save = undef;
  }
}
