| 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 - check for individual flag characters |
| 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 | # Process each file |
| 28 | for file_path in files: |
| 29 | try: |
| 30 | with open(file_path, 'r', encoding='utf-8') as file: |
| 31 | lines = file.readlines() |
| 32 | except FileNotFoundError: |
| 33 | # Edge Case: File not found - skip the file |
| 34 | continue |
| 35 | except IOError: |
| 36 | # Edge Case: File cannot be read - skip the file |
| 37 | continue |
| 38 | |
| 39 | # Process each line in the file |
| 40 | for line_num, line in enumerate(lines, 1): |
| 41 | # Prepare line for comparison |
| 42 | compare_line = line.lower() if case_insensitive else line |
| 43 | |
| 44 | # Check for match |
| 45 | if exact_match: |
| 46 | # Edge Case: Exact match flag - match entire line (excluding newline) |
| 47 | matches = compare_line.rstrip('\n') == search_pattern |
| 48 | else: |
| 49 | # Edge Case: Substring match - search pattern anywhere in line |
| 50 | matches = search_pattern in compare_line |
| 51 | |
| 52 | # Apply invert flag |
| 53 | if invert_match: |
| 54 | # Edge Case: Invert match flag - collect lines that don't match |
| 55 | matches = not matches |
| 56 | |
| 57 | # If line matches criteria |
| 58 | if matches: |
| 59 | if files_only: |
| 60 | # Edge Case: Files only flag - collect file names with matches |
| 61 | matching_files.add(file_path) |
| 62 | # No need to process more lines in this file |
| 63 | break |
| 64 | else: |
| 65 | # Build output line |
| 66 | output_line = "" |
| 67 | |
| 68 | # Add file name if multiple files |
| 69 | if len(files) > 1: |
| 70 | output_line += f"{file_path}:" |
| 71 | |
| 72 | # Add line number if requested |
| 73 | if line_numbers: |
| 74 | output_line += f"{line_num}:" |
| 75 | |
| 76 | # Add the actual line content |
| 77 | output_line += line |
| 78 | |
| 79 | results.append(output_line) |
| 80 | |
| 81 | # Return results based on flags |
| 82 | if files_only: |
| 83 | # Edge Case: Return only file names that contain matches |
| 84 | return "\n".join(sorted(matching_files)) |
| 85 | else: |
| 86 | # Edge Case: Return all matching lines |
| 87 | return "".join(results).rstrip('\n') |
| 88 | |
| 89 | # Handled Edge Cases: Empty files list, file not found, file cannot be read, exact match flag, substring match, invert match flag, files only flag, multiple files, line numbers flag, case insensitive flag |