Saturday, April 2, 2016

Fix Log Tag Printer in Python


#!/usr/bin/env python3
""" show fix log tag per line,e.g flt.py log.txt 52 49 55  """

import os
import sys
""" mod """
def get_tag_values(line,tags):
    tag_values=[]   
    for t in tags:
        if t+"=" in line:
            tag_values.append(line.split(t+"=")[1].split("=")[0])
    return tag_values

def show_tags_per_line(file,tags):
    os.chdir(".")
    with open(file) as lines:
        for line in lines:
            v=get_tag_values(line,tags)
            if len(v) >0:
                print(v)
                
def ask_help(argv):
    if len(argv)<2:
        pass
    v=argv[1]
    if v=="/h" or v=="/help" or v=="-h" or v=="-help":
        print(__doc__)
        sys.exit(0)
    
if __name__=='__main__':
    ask_help(sys.argv)
        
    tags=sys.argv[2:]
    file=sys.argv[1]
    show_tags_per_line(file,tags);

No comments:

Post a Comment