#!/usr/bin/python3 from subprocess import getoutput from sys import argv print("Argv: ", argv) verbose = False line_numbers = False if len(argv) < 2: print("Usage: " + argv[0] + " [-vl] search_term ") from sys import exit exit(1) for arg in argv: if arg[0] == "-": if "v" in arg: verbose = True if "l" in arg: line_numbers = True filenames = getoutput("dir").split() for arg in argv[1:]: if arg[0] != '-': search_term = arg for fn in filenames: # each filename is fn # Prints out the file and the number of lines inside it #print(fn, ": ", getoutput("cat " + fn + " | wc -l ")) if not ".py" in fn: continue try: file_object = open(fn) lines_from_file = file_object.readlines() except: if verbose: print("Can't read file: ", fn) # Now look for matches in the file! for line_number, line in enumerate(lines_from_file): if search_term in line: if not line_numbers: print(fn, ": ", line.strip()) else: print(fn, line_number, ": ", line.strip())