-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext_processing.sh
138 lines (120 loc) · 3.69 KB
/
text_processing.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
source helper_func.sh
source colours.sh
search_file() {
local file_path
local pattern
read -p "Enter the filename: " file_path
read -p "Enter the pattern to be searched for: " pattern
for l in $file_path; do
if file "$l" | grep -q text; then
flag=1
if grep -Eq "$pattern" "$l"; then
grep -HEns "$pattern" "$l" | less -r --prompt="Press q to exit"
else
echo -e "${RED}error: $l has no matching text${ENDCOLOUR}" | less -r
fi
fi
done
if [[ $flag != 1 ]]; then
echo -e "${RED}File not found${ENDCOLOUR}"
fi
read -n 1 -r -s -p $'\nPress any key to continue... '
clear
}
count_wc() {
local file_path
local flag
local lines
local words
local chars
read -p "Enter the path of the file: " file_path
for l in $file_path; do
if file "$l" | grep -q text; then
flag=1
lines="${COLOUR}$(wc -l $l | cut -d " " -f1)${ENDCOLOUR}"
words="${COLOUR}$(wc -w $l | cut -d " " -f1)${ENDCOLOUR}"
chars="${COLOUR}$(wc -m $l | cut -d " " -f1)${ENDCOLOUR}"
echo -e "$l has $lines lines, $words words and $chars characters.\n" | less -r --prompt="Press q to exit"
fi
done
if [[ $flag != 1 ]]; then
echo -e "${RED}File not found${ENDCOLOUR}"
fi
read -n 1 -r -s -p $'\nPress any key to continue... '
clear
}
display_diff() {
local file_1
local file_2
while true; do
local i=0
read -p "Enter the path of the first file: " file_1
for l in $file_1; do
i=$((i + 1))
done
if [[ $i -ne 1 ]]; then
echo -e "${RED}You've entered wrong filename or more than one file${ENDCOLOUR}"
continue
fi
if file "$file_1" | grep -q text; then
break
else
echo -e "${RED}error: The file is not a text file or cannot be found${ENDCOLOUR}"
fi
done
while true; do
local i=0
read -p "Enter the path of the second file: " file_2
for l in $file_2; do
i=$((i + 1))
done
if [[ $i -ne 1 ]]; then
echo -e "${RED}error: You've entered wrong filename or more than one file${ENDCOLOUR}"
continue
fi
if file "$file_2" | grep -q text; then
break
else
echo -e "${RED}error: The file is not a text file or cannot be found${ENDCOLOUR}"
fi
done
diff --color -y $file_1 $file_2 | less -r --prompt="Press q to exit"
clear
}
text_processing_menu() {
local selection
until [ "$selection" = "0" ]; do
center_text "${BGCOLOUR}TEXT MENU${ENDCOLOUR}" " "
echo -e "${COLOUR}1${ENDCOLOUR} -- Search a file for a pattern"
echo -e "${COLOUR}2${ENDCOLOUR} -- Count lines, words, and characters in specified files"
echo -e "${COLOUR}3${ENDCOLOUR} -- Display line differences between two files"
if [ $MYHELP_MODE = novice ]; then
echo -e "${COLOUR}4${ENDCOLOUR} -- Quit -- Return to main Menu\n"
else
echo -e "${COLOUR}4${ENDCOLOUR} -- Quit -- Exit Program\n"
fi
center_text_prompt "\b\b\b\b\b\b\bEnter your choice: " " "
read -n 1 -r selection
clear
case $selection in
1)
search_file
;;
2)
count_wc
;;
3)
display_diff
;;
q | 4)
exit 0
;;
*)
center_text "${RED}error: Enter correct choice [1-4]${ENDCOLOUR}"
selection=
;;
esac
done
}
text_processing_menu