| 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 (-n, -l, -i, -v, -x) |
| 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 number |
| 15 | -l: Output only file names |
| 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 | 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 | # Edge Case: No files provided |
| 36 | if not files: |
| 37 | return "" |
| 38 | |
| 39 | # Process each file |
| 40 | for filename in files: |
| 41 | try: |
| 42 | with open(filename, 'r', encoding='utf-8') as file: |
| 43 | lines = file.readlines() |
| 44 | |
| 45 | # Edge Case: File is empty or unreadable |
| 46 | if not lines: |
| 47 | # For empty files, we still need to check if they match (which they won't unless pattern is empty) |
| 48 | if filenames_only and pattern == "": |
| 49 | matching_files.add(filename) |
| 50 | continue |
| 51 | |
| 52 | # Process each line |
| 53 | for line_num, line in enumerate(lines, 1): |
| 54 | # Prepare line for comparison |
| 55 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 56 | |
| 57 | # Determine if line matches |
| 58 | if exact_match: |
| 59 | matches = (compare_line == search_pattern) |
| 60 | else: |
| 61 | matches = (search_pattern in compare_line) |
| 62 | |
| 63 | # Apply invert flag |
| 64 | if invert_match: |
| 65 | matches = not matches |
| 66 | |
| 67 | # Handle matches |
| 68 | if matches: |
| 69 | if filenames_only: |
| 70 | matching_files.add(filename) |
| 71 | # No need to process more lines in this file for -l flag |
| 72 | break |
| 73 | else: |
| 74 | # Build output line |
| 75 | output_parts = [] |
| 76 | |
| 77 | # Add filename prefix if multiple files |
| 78 | if len(files) > 1: |
| 79 | output_parts.append(filename) |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | # If we have filename, add colon, otherwise just the number |
| 84 | if output_parts: |
| 85 | output_parts.append(':') |
| 86 | output_parts.append(str(line_num)) |
| 87 | |
| 88 | # Add colon separator if we have filename or line number |
| 89 | if output_parts: |
| 90 | output_parts.append(':') |
| 91 | |
| 92 | # Add the actual line |
| 93 | output_parts.append(line.rstrip('\n')) |
| 94 | |
| 95 | results.append(''.join(output_parts)) |
| 96 | |
| 97 | # Edge Case: File not found or permission denied |
| 98 | except (FileNotFoundError, PermissionError): |
| 99 | # According to specification, we just skip files we can't read |
| 100 | continue |
| 101 | |
| 102 | # Edge Case: -l flag requested but no files matched |
| 103 | if filenames_only: |
| 104 | return '\n'.join(sorted(list(matching_files))) |
| 105 | |
| 106 | # Edge Case: No matches found |
| 107 | return '\n'.join(results) |
| 108 | |
| 109 | # Handled Edge Cases: No files provided, File is empty or unreadable, File not found or permission denied, -l flag requested but no files matched, No matches found |