| 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 flag characters (-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 | # Prepare pattern for comparison |
| 21 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 22 | |
| 23 | # Store results |
| 24 | results = [] |
| 25 | matching_files = set() |
| 26 | |
| 27 | # Edge Case: No files provided |
| 28 | if not files: |
| 29 | return "" |
| 30 | |
| 31 | # Process each file |
| 32 | for file_path in files: |
| 33 | try: |
| 34 | with open(file_path, 'r', encoding='utf-8') as file: |
| 35 | lines = file.readlines() |
| 36 | |
| 37 | # Edge Case: File cannot be read |
| 38 | except IOError: |
| 39 | continue |
| 40 | |
| 41 | # Process each line |
| 42 | for line_num, line in enumerate(lines, 1): |
| 43 | # Prepare line for comparison |
| 44 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 45 | |
| 46 | # Determine if line matches |
| 47 | if exact_match: |
| 48 | matches = compare_line == search_pattern |
| 49 | else: |
| 50 | matches = search_pattern in compare_line |
| 51 | |
| 52 | # Apply invert flag |
| 53 | if invert_match: |
| 54 | matches = not matches |
| 55 | |
| 56 | # If line matches |
| 57 | if matches: |
| 58 | # Edge Case: Only file names requested |
| 59 | if files_only: |
| 60 | matching_files.add(file_path) |
| 61 | break # Found a match, no need to check more lines |
| 62 | else: |
| 63 | # Build output line |
| 64 | output_line = "" |
| 65 | |
| 66 | # Add filename prefix if multiple files |
| 67 | # Edge Case: Single file should not have filename prefix |
| 68 | if len(files) > 1: |
| 69 | output_line += f"{file_path}:" |
| 70 | |
| 71 | # Add line number if requested |
| 72 | if line_numbers: |
| 73 | output_line += f"{line_num}:" |
| 74 | |
| 75 | # Add the actual line content |
| 76 | output_line += line |
| 77 | |
| 78 | results.append(output_line) |
| 79 | |
| 80 | # Edge Case: Files only flag takes precedence |
| 81 | if files_only: |
| 82 | # Return file names in the order they were found |
| 83 | ordered_files = [f for f in files if f in matching_files] |
| 84 | return "\n".join(ordered_files) + ("\n" if ordered_files else "") |
| 85 | |
| 86 | # Edge Case: No matches found |
| 87 | if not results: |
| 88 | return "" |
| 89 | |
| 90 | return "".join(results) |
| 91 | |
| 92 | # Handled Edge Cases: No files provided, File cannot be read, Only file names requested, Single file should not have filename prefix, Files only flag takes precedence, No matches found |