| 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: The string to search for |
| 7 | flags: Space-separated string of 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 | flag_list = flags.split() if flags else [] |
| 15 | line_numbers = '-n' in flag_list |
| 16 | files_only = '-l' in flag_list |
| 17 | case_insensitive = '-i' in flag_list |
| 18 | invert_match = '-v' in flag_list |
| 19 | exact_match = '-x' in flag_list |
| 20 | |
| 21 | # Prepare pattern for comparison |
| 22 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 23 | |
| 24 | # Results storage |
| 25 | results = [] |
| 26 | matching_files = set() |
| 27 | |
| 28 | # Edge Case: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | multiple_files = len(files) > 1 |
| 33 | |
| 34 | # Process each file |
| 35 | for file_path in files: |
| 36 | try: |
| 37 | with open(file_path, 'r', encoding='utf-8') as file: |
| 38 | lines = file.readlines() |
| 39 | |
| 40 | # Edge Case: File cannot be read |
| 41 | except IOError: |
| 42 | continue |
| 43 | |
| 44 | # Process each line in the file |
| 45 | for line_num, line in enumerate(lines, 1): |
| 46 | # Prepare line for comparison |
| 47 | compare_line = line.lower() if case_insensitive else line |
| 48 | |
| 49 | # Determine if line matches |
| 50 | if exact_match: |
| 51 | matches = search_pattern == compare_line.rstrip('\n') |
| 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 pattern |
| 60 | if matches: |
| 61 | # Edge Case: Only file names requested (-l flag) |
| 62 | if files_only: |
| 63 | matching_files.add(file_path) |
| 64 | break # Found a match, no need to check more lines |
| 65 | else: |
| 66 | # Build output line |
| 67 | output_line = "" |
| 68 | |
| 69 | # Add filename prefix if multiple files |
| 70 | if multiple_files: |
| 71 | output_line += f"{file_path}:" |
| 72 | |
| 73 | # Add line number if requested |
| 74 | if line_numbers: |
| 75 | output_line += f"{line_num}:" |
| 76 | |
| 77 | # Add the actual line content |
| 78 | output_line += line |
| 79 | |
| 80 | results.append(output_line) |
| 81 | |
| 82 | # Edge Case: Only file names requested and current file has matches |
| 83 | if files_only and file_path in matching_files: |
| 84 | continue |
| 85 | |
| 86 | # Edge Case: Only file names requested (-l flag) |
| 87 | if files_only: |
| 88 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 89 | |
| 90 | # Edge Case: No matches found |
| 91 | if not results: |
| 92 | return "" |
| 93 | |
| 94 | # Join all results with newlines |
| 95 | return "".join(results) |
| 96 | |
| 97 | # Handled Edge Cases: No files provided, File cannot be read, Only file names requested, No matches found |