| 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 customize the search behavior. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: All matching lines, formatted according to the flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names 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 | files_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 | results = [] |
| 29 | matched_files = set() |
| 30 | |
| 31 | # Edge Case: No files provided |
| 32 | if not files: |
| 33 | return "" |
| 34 | |
| 35 | # Edge Case: Pattern is empty |
| 36 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 37 | |
| 38 | for file_path in files: |
| 39 | try: |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | except FileNotFoundError: |
| 43 | # Edge Case: File not found - skip the file |
| 44 | continue |
| 45 | |
| 46 | file_has_match = False |
| 47 | |
| 48 | for line_num, line in enumerate(lines, start=1): |
| 49 | # Prepare line for comparison |
| 50 | compare_line = line.rstrip('\n') |
| 51 | if case_insensitive: |
| 52 | compare_line = compare_line.lower() |
| 53 | |
| 54 | # Determine if line matches |
| 55 | if exact_match: |
| 56 | is_match = (compare_line == search_pattern) |
| 57 | else: |
| 58 | is_match = (search_pattern in compare_line) |
| 59 | |
| 60 | # Apply invert flag |
| 61 | if invert_match: |
| 62 | is_match = not is_match |
| 63 | |
| 64 | if is_match: |
| 65 | file_has_match = True |
| 66 | |
| 67 | # Edge Case: Only file names requested |
| 68 | if files_only: |
| 69 | break # No need to process more lines in this file |
| 70 | |
| 71 | output_line = line.rstrip('\n') |
| 72 | |
| 73 | # Build output according to flags |
| 74 | if len(files) > 1: |
| 75 | output_line = f"{file_path}:{output_line}" |
| 76 | |
| 77 | if line_numbers: |
| 78 | if len(files) > 1: |
| 79 | output_line = f"{file_path}:{line_num}:{output_line.split(':', 1)[1]}" |
| 80 | else: |
| 81 | output_line = f"{line_num}:{output_line}" |
| 82 | |
| 83 | results.append(output_line) |
| 84 | |
| 85 | # Edge Case: File name should be recorded for -l flag |
| 86 | if files_only and file_has_match: |
| 87 | matched_files.add(file_path) |
| 88 | |
| 89 | # Edge Case: Return only file names if -l flag is set |
| 90 | if files_only: |
| 91 | return '\n'.join(sorted(matched_files)) + ('\n' if matched_files else '') |
| 92 | |
| 93 | return '\n'.join(results) + ('\n' if results else '') |
| 94 | # Handled Edge Cases: No files provided; Pattern is empty; File not found; Only file names requested; Multiple files with line numbers" |