add -m flag that prints only the match
diff --git a/demumble.cc b/demumble.cc
index f099b51..6095d15 100644
--- a/demumble.cc
+++ b/demumble.cc
@@ -23,19 +23,24 @@
   return strncmp(s, prefix, strlen(prefix)) == 0;
 }
 
-static void print_demangled(const char* s) {
+enum PrintMode { kPrintAll, kPrintMatching };
+static bool print_demangled(const char* s, PrintMode print_mode) {
   const char* cxa_in = s;
   if (starts_with(s, "__Z") || starts_with(s, "____Z"))
     cxa_in += 1;
   if (char* itanium = __cxa_demangle(cxa_in, NULL, NULL, NULL)) {
     printf("%s", itanium);
     free(itanium);
+    return true;
   } else if (char* ms = __unDName(NULL, s, 0, &malloc, &free, 0)) {
     printf("%s", ms);
     free(ms);
-  } else {
+    return true;
+  } else if (print_mode == kPrintAll) {
     printf("%s", s);
+    return true;
   }
+  return false;
 }
 
 static bool is_mangle_char_posix(char c) {
@@ -50,9 +55,15 @@
 
 static char buf[8192];
 int main(int argc, char* argv[]) {
+  PrintMode print_mode = kPrintAll;
+  if (argc > 1 && strcmp(argv[1], "-m") == 0) {
+    print_mode = kPrintMatching;
+    --argc;
+    ++argv;
+  }
   for (int i = 1; i < argc; ++i) {
-    print_demangled(argv[i]);
-    printf("\n");
+    if (print_demangled(argv[i], print_mode))
+      printf("\n");
   }
   if (argc == 1) {  // Read stdin instead.
     char c;
@@ -62,12 +73,17 @@
     // But type manglings can be regular words ("Pi" is "int*").
     // (For command-line args, do try to demangle types though.)
     while (fgets(buf, sizeof(buf), stdin)) {
+      bool need_separator = false;
       char* cur = buf;
       char* end = cur + strlen(cur);
 
       while (cur != end) {
         size_t special = strcspn(cur, "_?");
-        printf("%.*s", static_cast<int>(special), cur);
+        if (print_mode == kPrintAll)
+          printf("%.*s", static_cast<int>(special), cur);
+        else if (need_separator)
+          printf("\n");
+        need_separator = false;
         cur += special;
         if (cur == end)
           break;
@@ -82,7 +98,7 @@
 
         char tmp = cur[n_sym];
         cur[n_sym] = '\0';
-        print_demangled(cur);  // XXX don't print if not match
+        need_separator = print_demangled(cur, print_mode);
         cur[n_sym] = tmp;
 
         cur += n_sym;
diff --git a/demumble_test.py b/demumble_test.py
index 5f808d3..e21df99 100755
--- a/demumble_test.py
+++ b/demumble_test.py
@@ -7,6 +7,8 @@
     ('demumble ?Fx_i@@YAHP6AHH@Z@Z', 'int __cdecl Fx_i(int (__cdecl*)(int))\n'),
     ('demumble __Znwi', 'operator new(int)\n'),  # Strip extra _ (for OS X)
     ('demumble < __Znwi', 'operator new(int)\n'),  # Also from stdin
+    ('demumble -m hi _Z1fv ho _Z1gv', 'f()\ng()\n'),
+    ('demumble -m < hi _Z1fv ho _Z1gv', 'f()\ng()\n'),
 ]
 
 import os, subprocess
@@ -14,10 +16,10 @@
     cmd = t[0].split()
     # Assume that demumble is next to this script.
     cmd[0] = os.path.join(os.path.dirname(__file__) or '.', cmd[0])
-    if cmd[1] == '<':
-      p = subprocess.Popen(cmd[0], stdin=subprocess.PIPE,
+    if '<' in cmd:
+      p = subprocess.Popen(cmd[:cmd.index('<')], stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-      out = p.communicate(input='\n'.join(cmd[2:]) + '\n')[0]
+      out = p.communicate(input='\n'.join(cmd[cmd.index('<') + 1:]) + '\n')[0]
     else:
       out = subprocess.check_output(cmd)
     if out != t[1]: