| 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 | with open(file_path, 'r') as file: |
| 31 | lines = file.readlines() |
| 32 | |
| 33 | # Edge Case: File cannot be read |
| 34 | except IOError: |
| 35 | continue |
| 36 | |
| 37 | file_has_match = False |
| 38 | |
| 39 | for line_num, line in enumerate(lines, 1): |
| 40 | # Prepare line and pattern for comparison |
| 41 | search_line = line |
| 42 | search_pattern = pattern |
| 43 | |
| 44 | if case_insensitive: |
| 45 | search_line = line.lower() |
| 46 | search_pattern = pattern.lower() |
| 47 | |
| 48 | # Determine if line matches |
| 49 | if exact_match: |
| 50 | matches = search_line.rstrip('\n\r') == search_pattern |
| 51 | else: |
| 52 | matches = search_pattern in search_line |
| 53 | |
| 54 | # Apply invert flag |
| 55 | if invert_match: |
| 56 | matches = not matches |
| 57 | |
| 58 | if matches: |
| 59 | file_has_match = True |
| 60 | |
| 61 | # Edge Case: Only file names should be returned |
| 62 | if files_only: |
| 63 | break |
| 64 | |
| 65 | output_line = line |
| 66 | |
| 67 | # Edge Case: Multiple files - prepend filename |
| 68 | if multiple_files: |
| 69 | output_line = f"{file_path}:{output_line}" |
| 70 | |
| 71 | # Edge Case: Line numbers requested - prepend line number |
| 72 | if line_numbers: |
| 73 | # Find the position to insert the line number |
| 74 | if multiple_files: |
| 75 | # After the filename and colon |
| 76 | colon_pos = output_line.find(':') |
| 77 | output_line = f"{output_line[:colon_pos+1]}{line_num}:{output_line[colon_pos+1:]}" |
| 78 | else: |
| 79 | output_line = f"{line_num}:{output_line}" |
| 80 | |
| 81 | results.append(output_line.rstrip('\n') + '\n') |
| 82 | |
| 83 | # Edge Case: Files only flag - add file name if it has matches |
| 84 | if files_only and file_has_match: |
| 85 | results.append(f"{file_path}\n") |
| 86 | |
| 87 | # Edge Case: No matches found - return empty string |
| 88 | if not results: |
| 89 | return "" |
| 90 | |
| 91 | return ''.join(results) |
| 92 | |
| 93 | # Handled Edge Cases: No files provided, File cannot be read, Only file names should be returned, Multiple files - prepend filename, Line numbers requested - prepend line number, Files only flag - add file name if it has matches, No matches found - return empty string |