Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 73 lines (63 sloc) 2.14 KB
#!/usr/bin/env python
import optparse
import os
import sys
# this is a simple script to replace
# the majority of what I use awk for
# I got tired of typing all the extra characters
# $ echo "zero:one:two:three" | apy -F: -N, 3 2 1 0
# three,two,one,zero
# instead of
# $ echo "one:two:three:four" | awk -F: '{print $4","$3","$2","$1"}
#
# reverse order should work as well
# $ echo "zero one two three" | apy -- -1 -2 -3 0
# three two one zero
# this doesn't replace awk, just what I use it for mostly
if __name__ == "__main__":
delim = ' '
new_delim = ':'
parser = optparse.OptionParser()
parser.add_option('-F', '--field', dest="field", default=None, help="Field separator")
parser.add_option('-N', '--new', dest="new_field", default=' ', help="New field separator")
parser.add_option('-c', '--cut', dest="cut", action="store_true", help="each characater (think cut")
options, remainder = parser.parse_args()
if os.name == 'posix' and sys.stdout.isatty():
GREEN = '\x1b[32m'
RESET = '\x1b[0m'
else:
GREEN = ''
RESET = ''
for line in sys.stdin:
line = line.strip()
if options.cut:
a = list(line)
options.new_feild=''
else:
a = line.split(options.field)
if remainder == []:
fmt = '%s[%%d]%s%%s' % (GREEN, RESET)
new = [ fmt % (i) for i in enumerate(a) ]
else:
new = []
for n in remainder:
if ':' in n:
try:
# simple way to deal with blanks
if n.startswith(':'):
n = '0' + n
if n.endswith(':'):
n = n + '%s' % len(a)
b = [ int(i) for i in n.split(':') ]
for i in a[b[0]:b[1]]:
new.append(i)
except Exception, e:
print e
pass
else:
try:
b = int(n)
new.append(a[b])
except:
pass
print options.new_field.join(new)