#!/usr/bin/env python
#coding: utf-8
# 
# delf デリートフィールド（Open usp Tukubai版）
#
# designed by Nobuaki Tounaka
# written by Ryuichi Ueda
#
# The MIT License
#
# Copyright (C) 2012 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "delf <f1> <f2> ... [file]"
_version = "Sat Mar 10 17:18:43 JST 2012"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"

import re
import os
import sys

class Operator:
	#コンストラクタ
	def __init__(self):
		self.del_fields = []

	#この時点でファイル名と判別がついていること。
	def addField(self, field_str):

		p = field_str.split('/')
		if len(p) == 1:
			f = self.optionToField(p[0])
			t = f
		else:
			f = self.optionToField(p[0])
			t = self.optionToField(p[1])

		self.del_fields.append([f,t])

	def getOptionNum(self):
		return len(self.del_fields)

	def optionToField(self,pos):
		if pos[0] == "N":
			tmp = pos.split('-')
			if len(tmp) == 1:	return -1
			else:			return - 1 - int(tmp[1])
		else:
			return int(pos)

	def output(self, tokens):
		num = len(tokens)

		for f,t in self.del_fields:
			if f > 0:	f -= 1
			elif f < 0:	f += num
			else:		exit(0)
			if t > 0:	t -= 1
			elif t < 0:	t += num
			else:		exit(0)

			for i in range(f,t+1):
				tokens[i] = None

		tokens = [ e for e in tokens if e != None ]
		print " ".join(tokens).encode('utf_8')

def isFileName(s):
	reg = r'(\d+|NF(-\d+)?)((\.\d+){,2})(/(\d|NF(-\d+)?)\3)?$'

	p = re.compile(reg)
	return p.match(s) == None

def printUsage():
	print >> sys.stderr , "Usage    :", _usage
	print >> sys.stderr , "Version  :", _version
	print >> sys.stderr , "          ", _code
	sys.exit(1)

###########################################
#メイン関数
###########################################
if __name__ == '__main__':

	###########################################
	#オプションの処理
	###########################################

	#ファイル名を入れる
	filenames = []
	filename_pos = []

	argc = len(sys.argv)

	op = Operator()

	#オプションを一個ずつ見ていく。
	for i in range(1, argc):
		if isFileName(sys.argv[i]):
			filenames.append(sys.argv[i])
			filename_pos.append(i)
		else:
			op.addField(sys.argv[i])

	#フィールド指定がないときは usage を出す。
	if op.getOptionNum() == 0:
		printUsage()

	###########################################
	#ファイルを開く
	###########################################

	if len(filenames) == 0:
		file = sys.stdin
	elif len(filenames) == 1 and filename_pos[0] == argc-1:
		if filenames[0] == "-": 
			file = sys.stdin
		else:
			file = open(filenames[0], 'r')
	else:
		print >> sys.stderr, "ファイル指定が不正です。"
		sys.exit(1)

	###########################################
	#出力
	###########################################

	for line in file:
		#utf_8からユニコードに変換して改行を取る
		line = unicode(line, "utf_8").rstrip()
		tokens = [ x for x in line.split(' ') if x ]
		op.output(tokens)
