Skip to content

Commit b19b464

Browse files
committed
add grep clone tutorial
1 parent 07d02c3 commit b19b464

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
214214
- [How to Print Variable Name and Value in Python](https://www.thepythoncode.com/article/print-variable-name-and-value-in-python). ([code](python-standard-library/print-variable-name-and-value))
215215
- [How to Make a Hangman Game in Python](https://www.thepythoncode.com/article/make-a-hangman-game-in-python). ([code](python-standard-library/hangman-game))
216216
- [How to Use the Argparse Module in Python](https://www.thepythoncode.com/article/how-to-use-argparse-in-python). ([code](python-standard-library/argparse))
217+
- [How to Make a Grep Clone in Python](https://thepythoncode.com/article/how-to-make-grep-clone-in-python). ([code](python-standard-library/grep-clone))
217218

218219
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
219220
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Grep Clone in Python](https://thepythoncode.com/article/how-to-make-grep-clone-in-python)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Import the necessary libraries.
2+
import re, sys
3+
from colorama import init, Fore
4+
5+
# Initialize colorama.
6+
init()
7+
8+
# Grep function.
9+
def grep(pattern, filename):
10+
try:
11+
found_match = False
12+
with open(filename, 'r') as file:
13+
for line in file:
14+
if re.search(pattern, line):
15+
# Print matching lines in green.
16+
print(Fore.GREEN + line.strip() + "\n") # We are including new lines to enhance readability.
17+
found_match = True
18+
if not found_match:
19+
# Print message in red if no content is found.
20+
print(Fore.RED + f"No content found matching the pattern '{pattern}'.")
21+
except FileNotFoundError:
22+
# Print error message in red if the file is not found.
23+
print(Fore.RED + f"File '{filename}' not found.")
24+
25+
26+
if len(sys.argv) != 3:
27+
# Print usage message in red if the number of arguments is incorrect.
28+
print(Fore.RED + "Usage: python grep_python.py <pattern> <filename>")
29+
sys.exit(1)
30+
31+
pattern = sys.argv[1]
32+
filename = sys.argv[2]
33+
grep(pattern, filename)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy