From e6090d8ef44c0aa1e449115498472a3af0a3558c Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Tue, 16 Jul 2013 16:25:30 +0100 Subject: [PATCH] Add --index parameter to allow selection of nth occurence of field. --- abcfield.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/abcfield.py b/abcfield.py index 197b537..14fc64f 100755 --- a/abcfield.py +++ b/abcfield.py @@ -105,11 +105,15 @@ def convertTitle(t, options): return res def process(inf, options): + n = options.index for line in inf: line = line.strip() if len(line) > 2 and line[0] == options.field and line[1] == ':': - print(convertTitle(line[2:].strip(), options)) - break + if n > 1: + n = n - 1 + else: + print(convertTitle(line[2:].strip(), options)) + break parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" " Extract field data from ABC file.") @@ -121,6 +125,10 @@ parser.add_option("-m", "--html", dest="html", parser.add_option("-l", "--latex", dest="latex", action="store_true", default=False, help="format ouput for LaTeX") +parser.add_option("-n", "--index", dest="index", + action="store", type="int", default=1, + help="report INDEXth value [default: %default]", + metavar="INDEX") (options, args) = parser.parse_args() if options.html and options.latex: