Python way :-)
#!/bin/env python lv_bs = '001002003006007009' lv_es = '004005008010011012' lv_bss = [lv_bs[x:x+3] for x in range(0,len(lv_bs),3)] lv_ess = [lv_es[x:x+3] for x in range(0,len(lv_es),3)] swaps = 0 prev = '' line = '' ind = 1 for i in range(1,13): lv_item = '%03d' % (i) if lv_item in lv_bs: lv_type = 'b' else: lv_type = 's' if prev != '' and prev != lv_type: swaps += 1 if swaps < 2: line += lv_item prev = lv_type else: print 'Subseries S%d found\r\n' % ind prev = lv_type swaps = 0 line = lv_item ind += 1 print line print 'Subseries S%d found\r\n' % ind