| # Gaudi Framework, version v22r2 |

Home |  | Generated: Tue May 10 2011

Functions

# remove_lines Namespace Reference

## Functions
def | main

## Function Documentation

def remove_lines::main | ( | ) |

Definition at line 7 of file remove_lines.py.

    00008           :
    00009     if len(sys.argv) != 3:
    00010         print "Usage: %s <filename> <>\n" \
    00011               "\tRemoves from <filename> the lines matching <reg.exp.>" \
    00012               % os.path.basename(sys.argv[0])
    00013         sys.exit(1)
    00014
    00015     filename, pattern = sys.argv[1:]
    00016     if not os.path.isfile(filename):
    00017         print "Error: cannot find file '%s'" % filename
    00018         sys.exit(1)
    00019
    00020     try:
    00021         regexp = re.compile(pattern)
    00022     except re.error, v:
    00023         print "Error: invalid regular expression %r (%s)" % (pattern, v)
    00024         sys.exit(1)
    00025
    00026     # read the file in memory skipping the matched lines
    00027     lines = [ l
    00028               for l in open(filename) ]
    00029     orig_size = len(lines)
    00030     lines = [ l
    00031               for l in lines
    00032               if not regexp.search(l) ]
    00033     final_size = len(lines)
    00034     # rename the original to make a backup copy
    00035     if os.path.exists(filename + "~"):
    00036         os.remove(filename + "~")
    00037     os.rename(filename, filename + "~")
    00038     # write out the file
    00039     open(filename, "w").writelines(lines)
    00040     print "Removed %d lines out of %d in file %s, matching pattern %r" \
    00041           % (orig_size - final_size, orig_size, filename, pattern)

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines