| 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: 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 | # 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 | # Edge Case: File cannot be opened or read |
| 35 | with open(file_path, 'r', encoding='utf-8') as file: |
| 36 | lines = file.readlines() |
| 37 | except FileNotFoundError: |
| 38 | # Edge Case: File not found |
| 39 | continue |
| 40 | except IOError: |
| 41 | # Edge Case: Other I/O error when reading file |
| 42 | continue |
| 43 | |
| 44 | # Process each line |
| 45 | for line_num, line in enumerate(lines, 1): |
| 46 | # Prepare line for comparison |
| 47 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 48 | |
| 49 | # Determine if line matches |
| 50 | if exact_match: |
| 51 | matches = compare_line == search_pattern |
| 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, collect result |
| 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_parts = [] |
| 68 | |
| 69 | # Add filename prefix if multiple files |
| 70 | # Edge Case: Single file vs multiple files formatting |
| 71 | if len(files) > 1: |
| 72 | output_parts.append(file_path) |
| 73 | |
| 74 | # Add line number if requested |
| 75 | # Edge Case: Line number formatting with and without filename |
| 76 | if line_numbers: |
| 77 | output_parts.append(str(line_num)) |
| 78 | |
| 79 | # Join parts with colons |
| 80 | prefix = ':'.join(output_parts) |
| 81 | |
| 82 | # Add the actual line content |
| 83 | if prefix: |
| 84 | results.append(f"{prefix}:{line.rstrip()}" if line.endswith('\n') else f"{prefix}:{line}") |
| 85 | else: |
| 86 | results.append(line.rstrip() if line.endswith('\n') else line) |
| 87 | |
| 88 | # Edge Case: Only file names requested (-l flag) |
| 89 | if files_only: |
| 90 | return '\n'.join(sorted(matching_files)) + ('\n' if matching_files else '') |
| 91 | |
| 92 | # Edge Case: No matches found |
| 93 | return '\n'.join(results) + ('\n' if results else '') |
| 94 | |
| 95 | # Handled Edge Cases: No files provided, File not found, Other I/O error when reading file, Only file names requested (-l flag), Single file vs multiple files formatting, Line number formatting with and without filename, No matches found |