| 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 behavior (-n, -l, -i, -v, -x) |
| 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 storage |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Process each file |
| 36 | for filename in files: |
| 37 | try: |
| 38 | with open(filename, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: File not found - skip the file |
| 42 | continue |
| 43 | |
| 44 | # Process each line |
| 45 | for line_num, line in enumerate(lines, 1): |
| 46 | # Prepare line for comparison |
| 47 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 48 | |
| 49 | # Determine if line matches |
| 50 | if exact_match: |
| 51 | matches = (compare_line == search_pattern) |
| 52 | else: |
| 53 | matches = (search_pattern in compare_line) |
| 54 | |
| 55 | # Apply invert flag |
| 56 | if invert_match: |
| 57 | matches = not matches |
| 58 | |
| 59 | # If line matches, collect results |
| 60 | if matches: |
| 61 | # Edge Case: When using -l flag, we only need to know which files have matches |
| 62 | if filenames_only: |
| 63 | matching_files.add(filename) |
| 64 | break # No need to check more lines in this file |
| 65 | else: |
| 66 | # Build output line |
| 67 | output_line = "" |
| 68 | # Add filename prefix if multiple files |
| 69 | if len(files) > 1: |
| 70 | output_line += f"{filename}:" |
| 71 | # Add line number if requested |
| 72 | if line_numbers: |
| 73 | output_line += f"{line_num}:" |
| 74 | # Add the actual line content |
| 75 | output_line += line |
| 76 | results.append(output_line) |
| 77 | |
| 78 | # Edge Case: When using -l flag, return only filenames |
| 79 | if filenames_only: |
| 80 | return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "") |
| 81 | |
| 82 | # Edge Case: No matches found |
| 83 | if not results: |
| 84 | return "" |
| 85 | |
| 86 | # Return results |
| 87 | return "".join(results) |
| 88 | |
| 89 | # Handled Edge Cases: File not found, case insensitive matching, exact line matching, |
| 90 | # invert matching, line numbering, multiple files with prefixes, filenames only output, |
| 91 | # no matches found, empty flags, exact match with line endings |