Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active July 20, 2025 03:41
Show Gist options
  • Save hidsh/a3ee73315be6d654c0cc74f553d0c1a9 to your computer and use it in GitHub Desktop.
Save hidsh/a3ee73315be6d654c0cc74f553d0c1a9 to your computer and use it in GitHub Desktop.
A trivial python script to follow the `tail`ing logs such as `dmesg -w`
#!/usr/bin/env python3
#
# Example usage:
# dmesg -w | myfilter.py -x foo bar baz
#
import sys
import re
import argparse
def parse_args():
parser = argparse.ArgumentParser(
description="Filter out lines matching any of the given patterns."
)
parser.add_argument(
"-x", "--excludes", nargs="+", required=True,
help="Regex patterns to exclude (any match will skip the line)"
)
return parser.parse_args()
def main():
args = parse_args()
patterns = [re.compile(p) for p in args.excludes]
for line in sys.stdin:
if any(p.search(line) for p in patterns):
continue
print(line, end='')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment