|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +# Define the pattern for matching the lines |
| 5 | +pattern = re.compile(r'^%ignore\s+[a-zA-Z0-9_::]+::[a-zA-Z0-9_]+\(.*&&;\)$') |
| 6 | + |
| 7 | +# Function to search files recursively |
| 8 | +def search_files_in_directory(directory): |
| 9 | + matches = [] |
| 10 | + |
| 11 | + # Walk through all files in the directory and its subdirectories |
| 12 | + for root, _, files in os.walk(directory): |
| 13 | + for file in files: |
| 14 | + file_path = os.path.join(root, file) |
| 15 | + # Skip files that are not relevant (e.g., non-text files) |
| 16 | + if not file.endswith((".txt", ".cpp", ".h")): |
| 17 | + continue |
| 18 | + |
| 19 | + # Open and read each file |
| 20 | + try: |
| 21 | + with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: |
| 22 | + lines = f.readlines() |
| 23 | + for line in lines: |
| 24 | + # Check if the line matches the pattern |
| 25 | + if pattern.match(line.strip()): |
| 26 | + matches.append(line.strip()) |
| 27 | + except Exception as e: |
| 28 | + print(f"Could not read file {file_path}: {e}") |
| 29 | + |
| 30 | + return matches |
| 31 | + |
| 32 | +# Write the matches to an output file |
| 33 | +def write_matches_to_file(matches): |
| 34 | + with open('output.txt', 'w', encoding='utf-8') as output_file: |
| 35 | + for match in matches: |
| 36 | + output_file.write(match + '\n') |
| 37 | + |
| 38 | +# Main function |
| 39 | +if __name__ == "__main__": |
| 40 | + directory = '.' # Current directory |
| 41 | + print("Searching for pattern in files...") |
| 42 | + matches = search_files_in_directory(directory) |
| 43 | + |
| 44 | + if matches: |
| 45 | + write_matches_to_file(matches) |
| 46 | + print(f"Found {len(matches)} matching lines. Results saved in 'output.txt'.") |
| 47 | + else: |
| 48 | + print("No matches found.") |
0 commit comments