| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions
remove_lines Namespace Reference

## Functions
def | main

## Function Documentation

def remove_lines.main | ( |  | ) |

Definition at line 7 of file remove_lines.py.

7
8 def main():
9 if len(sys.argv) != 3:
10 print "Usage: %s <filename> <>\n" \
11 "\tRemoves from <filename> the lines matching <reg.exp.>" \
12 % os.path.basename(sys.argv[0])
13 sys.exit(1)
14
15 filename, pattern = sys.argv[1:]
16 if not os.path.isfile(filename):
17 print "Error: cannot find file '%s'" % filename
18 sys.exit(1)
19
20 try:
21 regexp = re.compile(pattern)
22 except re.error, v:
23 print "Error: invalid regular expression %r (%s)" % (pattern, v)
24 sys.exit(1)
25
26 # read the file in memory skipping the matched lines
27 lines = [ l
28 for l in open(filename) ]
29 orig_size = len(lines)
30 lines = [ l
31 for l in lines
32 if not regexp.search(l) ]
33 final_size = len(lines)
34 # rename the original to make a backup copy
35 if os.path.exists(filename + "~"):
36 os.remove(filename + "~")
37 os.rename(filename, filename + "~")
38 # write out the file
39 open(filename, "w").writelines(lines)
40 print "Removed %d lines out of %d in file %s, matching pattern %r" \
41 % (orig_size - final_size, orig_size, filename, pattern)