| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search files for lines matching a search string and return all matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern (str): The string to search for |
| 7 | flags (str): Space-separated flags that modify search behavior |
| 8 | files (list[str]): List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines formatted according to flags |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers |
| 15 | -l: Output only filenames with matches |
| 16 | -i: Case-insensitive matching |
| 17 | -v: Invert matching (return non-matching lines) |
| 18 | -x: Match entire line only |
| 19 | """ |
| 20 | # Parse flags |
| 21 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | filenames_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Prepare pattern for comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | results = [] |
| 32 | matching_files = set() |
| 33 | |
| 34 | # Process each file |
| 35 | for filename in files: |
| 36 | try: |
| 37 | with open(filename, 'r', encoding='utf-8') as f: |
| 38 | lines = f.readlines() |
| 39 | except FileNotFoundError: |
| 40 | # Edge Case: File not found - skip the file |
| 41 | continue |
| 42 | |
| 43 | # Process each line in the file |
| 44 | for line_num, line in enumerate(lines, 1): |
| 45 | # Prepare line for comparison |
| 46 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 47 | |
| 48 | # Determine if line matches |
| 49 | if exact_match: |
| 50 | matches = (compare_line == search_pattern) |
| 51 | else: |
| 52 | matches = (search_pattern in compare_line) |
| 53 | |
| 54 | # Apply invert flag |
| 55 | if invert_match: |
| 56 | matches = not matches |
| 57 | |
| 58 | # If line matches, process according to flags |
| 59 | if matches: |
| 60 | matching_files.add(filename) |
| 61 | |
| 62 | # If we only need filenames, we can break early |
| 63 | if filenames_only: |
| 64 | break |
| 65 | |
| 66 | # Build output line |
| 67 | output_line = "" |
| 68 | |
| 69 | # Add filename prefix if multiple files |
| 70 | if len(files) > 1: |
| 71 | output_line += f"{filename}:" |
| 72 | |
| 73 | # Add line number if requested |
| 74 | if line_numbers: |
| 75 | output_line += f"{line_num}:" |
| 76 | |
| 77 | # Add the actual line content |
| 78 | output_line += line |
| 79 | |
| 80 | results.append(output_line) |
| 81 | |
| 82 | # If we only need filenames and we found a match, we can move to next file |
| 83 | if filenames_only and filename in matching_files: |
| 84 | continue |
| 85 | |
| 86 | # If filenames only flag is set, return just the filenames |
| 87 | if filenames_only: |
| 88 | # Edge Case: No matching files - return empty string |
| 89 | matching_files_list = sorted(list(matching_files)) |
| 90 | return "\n".join(matching_files_list) + ("\n" if matching_files_list else "") |
| 91 | |
| 92 | # Edge Case: No matching lines - return empty string |
| 93 | return "".join(results) |
| 94 | |
| 95 | # Handled Edge Cases: File not found, No matching files, No matching lines |