Commits

Alexander Solovyov committed 9b40223

ability to eliminate lines by pattern from search

  • Participants
  • Parent commits 65949b1

Comments (0)

Files changed (1)

  • +12 -4
    sr
 
     parser.add_option('-s', '--search', dest='search',
                       help='what to search')
+    parser.add_option('-e', '--eliminate', dest='eliminate',
+                      help='what to eliminate from search')
     parser.add_option('-r', '--replace', dest='replace',
                       help='what replace with')
     parser.add_option('-i', '--ignore-case', action='store_true',
         if opts.ignore_case:
             flags |= re.I
         config.search = re.compile(config.search, flags)
-
+        config.eliminate = re.compile(opts.eliminate, flags)
 
 def relpath(path):
     curpath = os.getcwd() + '/'
         return path[len(curpath):]
     return path
 
+def search_line(line):
+    return config.search.search(line) and not config.eliminate.search(line)
+
 def search(path):
     """
     Search pattern in each line of file.
 
     for count, line in enumerate(file(path)):
         number = count + 1
-        if config.search.search(line):
+        if search_line(line):
             if not started:
                 print config.term.highlight(relpath(path), 'GREEN')
                 if config.filenames:
                     break
                 started = True
             if len(line) <= config.output_limit:
-                print '%d:%s' % (number, config.term.highlight(line.rstrip('\n\r'), ('BLACK', 'BG_YELLOW'), config.search))
+                print '%d:%s' % (number,
+                                 config.term.highlight(line.rstrip('\n\r'),
+                                                       ('BLACK', 'BG_YELLOW'),
+                                                       config.search))
             else:
                 print '%d:LINE IS TOO LONG (>%d)' % (number, config.output_limit)
     if started:
 
     data = file(path).read()
 
-    if config.search.search(data):
+    if search_line(data):
         print path
 
         # Read and create new data