#! /usr/bin/python
#
#   GRAWL : grep and awk some lines from a text file
#
#   Combining the power of grep awk head/tail and cut
#   Usually to get numbers out of a file, but simple
#
#   Usage: grawl file [-i|-v|....] "pattern"     line|0   words|0  
#                      <-----   grep   ----->   head/tail    awk 
#                      
#
#  @todo
#      allow to bypass the grep with --, so it can quickly just grab lines from a file
#
#  growl: a mac tool , not related.

import sys

def grab(file):
    fp = open(file,'r')
    lines = fp.readlines()
    fp.close()
    return lines

def parselist(words):
    """ parse the various allowed options in which words to print
        using python notation
        k1        just the k1-th line (k1=0 being the first)
        k1:k2     k1 until k2 (but excluding k2)
        k2:k2:k3  k1 until k2 (excluding k2) in steps of k3
        :k2:k3    -same , but k1=0
        :k2       -same, but k1=0 and k3=1
        """

if __name__ == '__main__':
    nargs = len(sys.argv)
    if nargs == 5:
        filename = sys.argv[1]
        pattern  = sys.argv[2]
        l        = sys.argv[3]    
        w        = sys.argv[4]
        #
        w1 = int(w)
        l1 = int(l)
        #
        lines = grab(filename)
        for line in lines:
            i = line.find(pattern)
            if i<0: continue
            words = line.strip().split()
            print words[w1]
            # todo: only l1
    elif nargs == 4:
        filename = sys.argv[1]
        pattern  = sys.argv[2]
        l1       = sys.argv[3]    
        #
        lines = grab(filename)
        for line in lines:
            i = line.find(pattern)
            if i<0: continue
            print line.strip()
            # todo: only l1
    elif nargs == 3:
        filename = sys.argv[1]
        pattern  = sys.argv[2]
        lines = grab(filename)
        for line in lines:
            i = line.find(pattern)
            if i<0: continue
            print line.strip()
    else:
        print "Usage:  grawl file pattern [line0# [word0#]]"
