-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygrep.sh
More file actions
66 lines (56 loc) · 1.22 KB
/
mygrep.sh
File metadata and controls
66 lines (56 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
show_help() {
echo "Usage: $0 [OPTIONS] search_string filename"
echo "Options:"
echo " -n Show line numbers with output lines"
echo " -v Invert match (show lines that do not match)"
echo " --help Show this help message"
}
if [[ $# -lt 2 ]]; then
echo "Error: Not enough arguments."
show_help
exit 1
fi
if [[ "$1" == "--help" ]]; then
show_help
exit 0
fi
options=""
search=""
file=""
if [[ "$1" == -* ]]; then
options="$1"
search="$2"
file="$3"
else
search="$1"
file="$2"
fi
if [[ -z "$search" || -z "$file" ]]; then
echo "Error: Missing search string or filename."
show_help
exit 1
fi
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' not found."
exit 1
fi
linenumber=0
while IFS= read -r line; do
linenumber=$((linenumber + 1))
if echo "$line" | grep -iq "$search"; then
match=true
else
match=false
fi
if [[ "$options" == *v* ]]; then
match=$(! $match && echo true || echo false)
fi
if [[ "$match" == true ]]; then
if [[ "$options" == *n* ]]; then
echo "${linenumber}:$line"
else
echo "$line"
fi
fi
done < "$file"