#!/usr/bin/python
#
# compress rows with similar columns, showing only the first and last job if there are
# more than 5 similiar jobs following each other.
#
# Usage:  showq | rowcompressor 1,2,3
# Compress rows showing consecutive jobs with the same user, state and cpucount.
#
# Requirements: python v2.4 or higher.
# License: GPL V2
# Author: Roy Dragseth <roy.dragseth@cc.uit.no>

import itertools as it
import sys

numcols = map(int,sys.argv[1].split(','))

def keyfunc(a):
    global numcols
    columns = a.split()
    key = str()
    if len(columns) > max(numcols):
        for c in numcols:
            key+=columns[c]
    else:
        key = None
    
    return key

textrows = sys.stdin.readlines() # open("/tmp/showq.txt",'r').readlines() #

for k,g in it.groupby(textrows,keyfunc):
    rowgroup = list(g)
    if len(rowgroup) > 5:
        print rowgroup[0],
        print "         Skipping %s jobs" %(len(rowgroup)-2)
        print rowgroup[-1],
    else:
        for row in rowgroup:
            print row,
