#!/usr/bin/env python

import argparse
import os
import sys

def MakeColumnGetter(spec):
  column, colon, slice_spec = spec.partition(':')
  try:
    column = int(column) - 1
  except ValueError:
    raise argparse.ArgumentTypeError('invalid column spec %s' % spec)
  if not colon:
    return lambda s: s[column]
  if not os.isatty(sys.stderr.fileno()):
    raise argparse.ArgumentTypeError('refusing insane mutator on non-interactive shell')
  return eval('lambda s: s[%d][%s]' % (column, slice_spec))

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--delimeter', default=' ')
parser.add_argument('-o', '--output-delimeter', default=' ')
parser.add_argument('-S', '--no-strip', default=True, dest='strip', action='store_false')
parser.add_argument('column', nargs='+', type=MakeColumnGetter)
options = parser.parse_args()

for line in sys.stdin:
  if options.strip:
    line = line.strip()
  columns = line.split(options.delimeter)
  output = []
  for ColumnGetter in options.column:
    output.append(ColumnGetter(columns))
    output.append(options.output_delimeter)
  output[-1] = '\n' # kinda hacky way to make the last output delimeter a newline
  sys.stdout.write("".join(output))
