-
Notifications
You must be signed in to change notification settings - Fork 0
/
textman.sh
90 lines (75 loc) · 1.96 KB
/
textman.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# Function to choose a text file
choose_file() {
echo "Please enter the path to the text file:"
read -r file_path
if [[ ! -f $file_path ]]; then
echo "File does not exist. Please try again."
exit 1
fi
echo "File selected: $file_path"
}
# Function to find words based on user's choice
find_words() {
echo "Do you want to search for words that (start) or (end) with a specific string? (Enter 'start' or 'end'):"
read -r choice
if [[ $choice != "start" && $choice != "end" ]]; then
echo "Invalid choice. Please try again."
return
fi
echo "Please enter the string to search for:"
read -r search_string
if [[ $choice == "start" ]]; then
echo "Words starting with '$search_string':"
grep -o "\b$search_string\w*\b" "$file_path"
else
echo "Words ending with '$search_string':"
grep -o "\b\w*$search_string\b" "$file_path"
fi
}
# Function to count occurrences of a specific letter
count_letter() {
echo "Please enter the letter you want to count:"
read -r letter
if [[ ${#letter} -ne 1 ]]; then
echo "Please enter a single letter."
return
fi
count=$(grep -o "$letter" "$file_path" | wc -l)
echo "The letter '$letter' occurs $count times in the file."
}
count_string() {
echo "Please enter the string you want to count:"
read -r string
count=$(grep -o "$string" "$file_path" | wc -l)
echo "The string '$string' occurs $count times in the file."
}
main_menu() {
while true; do
echo "Do you want to (1) search for words, (2) count a string or (3) count a letter? (Enter '1','2' or '3'):"
read -r action
case $action in
1)
find_words
;;
2)
count_string
;;
3)
count_letter
;;
q)
echo "Exiting script."
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
echo "Returning to main menu..."
echo ""
done
}
# Main script execution
choose_file
main_menu