From 909a3c519bf217bf22e27caf2e93396e28beaaeb Mon Sep 17 00:00:00 2001 From: dmarakom6 Date: Fri, 2 Jul 2021 18:19:01 +0300 Subject: [PATCH] Run grep in files --- util/notegrep/m.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/util/notegrep/m.py b/util/notegrep/m.py index 7e088fa..1361125 100644 --- a/util/notegrep/m.py +++ b/util/notegrep/m.py @@ -9,6 +9,8 @@ import sys import os.path from os import path import subprocess +from subprocess import check_output +import re class Multi(): @@ -19,13 +21,19 @@ class Multi(): def print_matches(self): for file in self.files: - if file == "": return 0 - print(file if path.exists('Notes/'+file) else f"{file} (not found):\n-") - print("=" * len(file) if path.exists('Notes/'+file) else "") + try: + if file == "": return 0 + print("\n"+file if path.exists('Notes/'+file) else f"{file} (not found):\n-") + print("=" * len(file) if path.exists('Notes/'+file) else "") - for pattern in self.patterns: - if pattern == "": return 0 - #run grep... + for pattern in self.patterns: + if pattern == "": return 0 + with open('Notes/'+file, 'r') as f: + for line in f: + if re.search(pattern, line): + out = os.system(f"""echo -n "'{pattern}'": && grep -n --color=always {pattern} Notes/{file}""") + #bug: pattern is generated multiple times + except FileNotFoundError: pass def main():