| 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 modify search behavior |
| 8 | files (list[str]): List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers prepended |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers |
| 15 | -l: Output only file names |
| 16 | -i: Case-insensitive matching |
| 17 | -v: Invert matching |
| 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 | filenames_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 | # Prepare pattern for comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | # Results storage |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Process each file |
| 36 | for filename in files: |
| 37 | try: |
| 38 | with open(filename, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: File not found - skip the file |
| 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().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 48 | |
| 49 | # Determine if line matches |
| 50 | if exact_match: |
| 51 | # Edge Case: Exact match flag - match entire line |
| 52 | matches = (compare_line == search_pattern) |
| 53 | else: |
| 54 | # Edge Case: Partial match - search pattern anywhere in line |
| 55 | matches = (search_pattern in compare_line) |
| 56 | |
| 57 | # Apply invert flag |
| 58 | if invert_match: |
| 59 | # Edge Case: Invert match flag - flip match result |
| 60 | matches = not matches |
| 61 | |
| 62 | # If line matches, collect results |
| 63 | if matches: |
| 64 | matching_files.add(filename) |
| 65 | |
| 66 | # If only filenames are needed, we don't need to collect lines |
| 67 | if not filenames_only: |
| 68 | output_line = line.rstrip('\n') |
| 69 | |
| 70 | # Build the output prefix |
| 71 | prefix_parts = [] |
| 72 | |
| 73 | # Add filename if multiple files |
| 74 | if len(files) > 1: |
| 75 | # Edge Case: Multiple files - prepend filename |
| 76 | prefix_parts.append(filename) |
| 77 | |
| 78 | # Add line number if requested |
| 79 | if line_numbers: |
| 80 | # Edge Case: Line numbers flag - prepend line number |
| 81 | prefix_parts.append(str(line_num)) |
| 82 | |
| 83 | # Combine prefix with line |
| 84 | if prefix_parts: |
| 85 | output_line = ':'.join(prefix_parts) + ':' + output_line |
| 86 | |
| 87 | results.append(output_line) |
| 88 | |
| 89 | # If filenames only flag is set, return just the filenames |
| 90 | if filenames_only: |
| 91 | # Edge Case: Filenames only flag - return matching filenames |
| 92 | # Preserve order of files as provided |
| 93 | ordered_matching_files = [f for f in files if f in matching_files] |
| 94 | return '\n'.join(ordered_matching_files) |
| 95 | |
| 96 | # Return all matching lines |
| 97 | return '\n'.join(results) |
| 98 | |
| 99 | # Handled Edge Cases: File not found, exact match flag, partial match, invert match flag, multiple files, line numbers flag, filenames only flag, case insensitive matching |