| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search for lines matching a pattern in files and return matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern: The string to search for |
| 7 | flags: String containing command line flags (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing all matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags |
| 14 | line_numbers = '-n' in flags |
| 15 | files_only = '-l' in flags |
| 16 | case_insensitive = '-i' in flags |
| 17 | invert_match = '-v' in flags |
| 18 | exact_match = '-x' in flags |
| 19 | |
| 20 | results = [] |
| 21 | |
| 22 | # Edge Case: No files provided |
| 23 | if not files: |
| 24 | return "" |
| 25 | |
| 26 | multiple_files = len(files) > 1 |
| 27 | |
| 28 | for file_path in files: |
| 29 | try: |
| 30 | # Edge Case: File cannot be opened or doesn't exist |
| 31 | with open(file_path, 'r') as file: |
| 32 | lines = file.readlines() |
| 33 | except IOError: |
| 34 | # Skip files that cannot be read |
| 35 | continue |
| 36 | |
| 37 | file_has_match = False |
| 38 | |
| 39 | # Edge Case: Empty file |
| 40 | if not lines: |
| 41 | # Even if empty, we need to check if we should include it in -l output |
| 42 | if files_only and not invert_match: |
| 43 | # An empty file never contains a match, so don't include it |
| 44 | continue |
| 45 | |
| 46 | for line_num, line in enumerate(lines, 1): |
| 47 | # Remove newline for processing but keep track of it |
| 48 | line_content = line.rstrip('\n') |
| 49 | |
| 50 | # Determine if line matches based on pattern and flags |
| 51 | if case_insensitive: |
| 52 | match = pattern.lower() in line_content.lower() |
| 53 | if exact_match: |
| 54 | match = pattern.lower() == line_content.lower() |
| 55 | else: |
| 56 | match = pattern in line_content |
| 57 | if exact_match: |
| 58 | match = pattern == line_content |
| 59 | |
| 60 | # Apply invert flag |
| 61 | if invert_match: |
| 62 | match = not match |
| 63 | |
| 64 | if match: |
| 65 | file_has_match = True |
| 66 | |
| 67 | # Edge Case: -l flag only requires file names, not lines |
| 68 | if files_only: |
| 69 | # We found a match, no need to check more lines in this file |
| 70 | break |
| 71 | |
| 72 | output_line = "" |
| 73 | |
| 74 | # Add file name prefix if multiple files |
| 75 | if multiple_files: |
| 76 | output_line += f"{file_path}:" |
| 77 | |
| 78 | # Add line number if requested |
| 79 | if line_numbers: |
| 80 | output_line += f"{line_num}:" |
| 81 | |
| 82 | # Add the line content |
| 83 | output_line += line_content |
| 84 | |
| 85 | results.append(output_line) |
| 86 | |
| 87 | # Edge Case: -l flag with file match |
| 88 | if files_only and file_has_match: |
| 89 | results.append(file_path) |
| 90 | |
| 91 | # Join results with newlines |
| 92 | return "\n".join(results) + ("\n" if results else "") |
| 93 | # Handled Edge Cases: No files provided, File cannot be opened or doesn't exist, Empty file, -l flag only requires file names, not lines, -l flag with file match |