#!/usr/bin/env python
# Exercise 8
# cut command

import re
import sys
import itertools
import argparse


def convert(field):
    field = list(map(lambda f: int(f), field.split("-")))
    if len(field) == 1:
        return field
    else:
        for i in range(field[0], field[-1]):
            field.append(i)
        return sorted(set(field))


def parse(fields):
    try:
        fields = list(map(convert, fields.split(",")))
        fields = list(itertools.chain(*fields))
        if 0 in fields:
            print("cut: fields are numbered from 1")
            sys.exit(1)
        return list(map(lambda n: n - 1, fields))
    except Exception:
        print(f"cut: valor de campo inválido: «{fields}»")
        sys.exit(1)


parser = argparse.ArgumentParser()
parser.add_argument("stream", default=sys.stdin, nargs="?")
parser.add_argument("-d", "--delimiter", default=False, type=str)
parser.add_argument("-f", "--fields", type=str, required=True)
args = parser.parse_args()

for line in args.stream:
    line = line.strip()
    if args.delimiter is False:
        print(line)
    else:
        fields = re.compile(f"{args.delimiter}+").split(line)
        res = []
        for col in parse(args.fields):
            if col < len(fields):
                res.append(fields[col])
        print(" ".join(res))
