diff --git a/tgrepcsv.py b/tgrepcsv.py index 6c0cfcb..49828b8 100755 --- a/tgrepcsv.py +++ b/tgrepcsv.py @@ -3,11 +3,11 @@ # Copyright (c) 2012 TundraWare Inc., Des Plaines, IL 60018 USA # All Rights Reserved. For Terms Of Use See: tgrepcsv-license.txt # For Program Updates See: http://www.tundraware.com/Software/tgrepcsv -# $Id: tgrepcsv.py,v 1.100 2012/09/05 18:10:11 tundra Exp $ +# $Id: tgrepcsv.py,v 1.101 2012/09/05 18:30:41 tundra Exp $ # Embed the source control ID string for use by program -CVSID='$Id: tgrepcsv.py,v 1.100 2012/09/05 18:10:11 tundra Exp $' +CVSID='$Id: tgrepcsv.py,v 1.101 2012/09/05 18:30:41 tundra Exp $' ##### # Program Information @@ -29,7 +29,7 @@ # List Of All Legal Options - Update This When You Add More!!!! ##### -OPTIONSLIST = '-f:hv' +OPTIONSLIST = '-hv' #----------------------------------------------------------# @@ -45,10 +45,10 @@ # Imports # #----------------------------------------------------------# +import csv import getopt import os import sys -# import tconfpy # Uncomment this and code below to parse config files #----------------------------------------------------------# @@ -98,6 +98,7 @@ eBADARG = "Invalid command line: %s!" eERROR = "ERROR" +eFEWARGS = "Too few commmand line arguments!" ##### @@ -112,9 +113,8 @@ ##### uTable = [PROGVER, - "usage: " + PROGNAME + " [-fhv]", + "usage: " + PROGNAME + " [-hv]", " where,", - " -f file configuration file to use", " -h print this help information", " -v print detailed version information", ] @@ -124,8 +124,6 @@ # Global Variables & Data Structures # #----------------------------------------------------------# -CFGFILE = os.path.join(os.getenv("HOME"), "." + "tgrepcsv") # conf file - #--------------------------- Code Begins Here ---------------------------------# @@ -255,33 +253,45 @@ for opt, val in opts: - if opt == "-f": - CFGFILE=val - if opt == "-h": Usage() sys.exit(0) if opt == "-v": - print CVSID + PrintStdout(CVSID) sys.exit(0) -# Processing of the configuration file below requires installation of -# the freely available TundraWare Inc. 'tconfpy' parser and then -# uncommenting the code below. The parser can be found at: -# -# http://www.tundraware.com/Software/tconfpy -# -# Process the configuration file -# -#retval = tconfpy.ParseConfig(CFGFILE, CallingProgram="%s %s " % (PROGNAME, VERSION)) -# -# Print any errors or warning generated by the parse -# -# for x in (retval.ErrMsgs, retval.WarnMsgs): -# for y in x: -# print y -# -# If there were any errors, we're done -#if retval.ErrMsgs: -# sys.exit(0) + +# Check and parse command line args + +if len(sys.argv) < 3: + + PrintStderr(eFEWARGS) + sys.exit(1) + +CSVFile = sys.argv[1] +Patterns = sys.argv[2:] + + +# Read the file into a local list + +content = [] +with open(CSVFile, 'rb') as f: + + reader = csv.reader(f) + for row in reader: + content.append(row) + + +# Output lines matching any of the expressions + +for row in content: + + line = " ".join(row) + match = False + for pattern in Patterns: + if pattern in line: + match = True + + if match: + PrintStdout(line)