#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# Given a column number and input *sorted by that column*,
# 'remove_singleton_lines' removes all "blocks" of lines that appear just once.
#
# $Id: remove_singleton_lines,v 1.1 2013-04-10 07:56:52 bojar Exp $
#

use strict;

my $col = shift;
die "remove_singleton_lines <colnumber>  < input > blocks" if !$col;

$col--;

my $limit = 1;

my $lastval;
my $block = "";
my $linesinblock = 0;
while (<>) {
  my @line = split /\t/, $_;
  # print STDERR "E: $lastval vs. $line[$col]; $col\n";
  if (defined $lastval && $lastval ne $line[$col]) {
    print $block if $linesinblock > $limit;
    $block = "";
    $linesinblock = 0;
  }
  $lastval = $line[$col];
  $block .= $_;
  $linesinblock++;
}
print $block if $linesinblock > $limit;
