import sys
import tempfile
import os
from subprocess import call
import argparse


guide = """# Remove or modify lines.
# Lines that are prefixed with the # character are filtered out.
# When you are done, save the file and exit.
"""

description = """Interactively filter lines in a pipe.

Example: Delete selected files in a directory

    find . -type f | ifilter | xargs rm
"""


def get_editor():
    if "EDITOR" in os.environ:
        return os.environ["EDITOR"]

    if "VISUAL" in os.environ:
        return os.environ["VISUAL"]

    return "vi"


def main():
    parser = argparse.ArgumentParser(
        prog='ifilter',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=description)

    # Currently args are unused
    args = parser.parse_args()

    s = sys.stdin.read()

    f = tempfile.NamedTemporaryFile(delete=False)
    f.write(guide)
    f.write(s)
    f.close()

    editor = get_editor()

    call("</dev/tty >/dev/tty %s %s " % (editor, f.name), shell=True)

    with open(f.name, "r") as f:
        for line in f.readlines():
            if not line.startswith("#"):
                print line,

    os.remove(f.name)


if __name__ == "__main__":
    main()

@@ -557,24 +557,47 @@
 def main():%0A
+%0A    try:%0A        %0A    
     parser =
@@ -630,16 +630,20 @@
         
+ 
+   
 prog='if
@@ -650,16 +650,20 @@
 ilter',%0A
+    
         
@@ -724,16 +724,20 @@
         
+    
 descript
@@ -758,16 +758,20 @@
 n)%0A%0A    
+ 
+   
 # Curren
@@ -794,16 +794,20 @@
 sed%0A    
+   
+ 
 args = p
@@ -822,24 +822,28 @@
 rse_args()%0A%0A
+    
     s = sys.
@@ -852,24 +852,28 @@
 din.read()%0A%0A
+    
     f = temp
@@ -910,16 +910,20 @@
 =False)%0A
+    
     f.wr
@@ -933,16 +933,20 @@
 (guide)%0A
+    
     f.wr
@@ -952,16 +952,20 @@
 rite(s)%0A
+    
     f.cl
@@ -967,24 +967,28 @@
  f.close()%0A%0A
+    
     editor =
@@ -1006,16 +1006,20 @@
 ()%0A%0A    
+   
+ 
 call(%22%3C/
@@ -1077,16 +1077,20 @@
 =True)%0A%0A
+    
     with
@@ -1122,16 +1122,20 @@
         
+ 
+   
 for line
@@ -1165,16 +1165,20 @@
         
+   
+ 
 if not l
@@ -1214,16 +1214,20 @@
         
+    
 print li
@@ -1230,17 +1230,115 @@
 t line,%0A
-%0A
+    finally:                                                    %0A        if f is not None:%0A        
     os.r

Add finally block for deletion of temp file