-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselectfromtb.sh
175 lines (145 loc) · 7.05 KB
/
selectfromtb.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
select_columns_by_name() {
local cols=$1
local all_col_names=($(awk -F, '{print $1}' "${TBName}.meta"))
local selected_headers=""
local col_nums=""
IFS=',' read -r -a col_names <<< "$cols"
for col_name in "${col_names[@]}"; do
local col_index=0
local found=false
for header_col in "${all_col_names[@]}"; do
((col_index++))
if [[ "$header_col" == "$col_name" ]]; then
selected_headers+="$col_name,"
col_nums+="$col_index,"
found=true
break
fi
done
if ! $found; then
echo "Error: Column '$col_name' does not exist."
return 1
fi
done
selected_headers=${selected_headers%,}
col_nums=${col_nums%,}
echo "$selected_headers"
echo "$col_nums"
}
source ~/project/utils.sh
get_table_name
select option in "Show Column Names" "Select All Columns" "Select Specific Columns" "Exit"; do
case $option in
"Show Column Names")
awk -F, '{print NR, $1}' "${TBName}.meta"
;;
"Select All Columns")
while true; do
read -p "Do You Want to Apply Where Condition? [y/n]: " wcond
if [[ $wcond =~ ^[Nn]$ ]]; then
headers=$(awk -F, '{print $1}' "${TBName}.meta" | paste -sd, | awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}')
echo "$headers"
echo "----------------------------------------------------------------------------"
awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}' "$TBName"
break
elif [[ $wcond =~ ^[Yy]$ ]]; then
read -p "Please Enter Your Condition (e.g., column_name = value): " wcol
if ! [[ $wcol =~ ^[A-Za-z0-9_]+[[:space:]]+(=|!=|>=|<=|>|<)[[:space:]]+.*$ ]]; then
echo "Invalid Condition"
continue
fi
column_name=$(echo "$wcol" | cut -d' ' -f1)
operator=$(echo "$wcol" | cut -d' ' -f2)
column_value=$(echo "$wcol" | cut -d' ' -f3)
col_num=$(awk -F, -v col="$column_name" '{if ($1 == col) {print NR; exit}}' "${TBName}.meta")
if [[ -n $col_num ]]; then
headers=$(awk -F, '{print $1}' "${TBName}.meta" | paste -sd, | awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}')
echo "$headers"
echo "----------------------------------------------------------------------------"
awk -F, -v col="$col_num" -v op="$operator" -v val="$column_value" '
{
condition_met = 0;
if (op == "=" && $col == val) condition_met = 1;
else if (op == "!=" && $col != val) condition_met = 1;
else if (op == ">" && $col > val) condition_met = 1;
else if (op == "<" && $col < val) condition_met = 1;
else if (op == ">=" && $col >= val) condition_met = 1;
else if (op == "<=" && $col <= val) condition_met = 1;
if (condition_met) {
for (i=1; i<=NF; i++) printf "%-15s", $i;
print ""
}
}' "$TBName"
break
else
echo "Error: Column '$column_name' does not exist.make sure to put >Spaces< before and after the operator."
fi
else
echo "Invalid option. Please choose [y/n]."
fi
done
;;
"Select Specific Columns")
echo "Selecting specific columns by name."
read -p "Which columns do you want to select (e.g., col1,col2): " cols
if ! [[ "$cols" =~ ^([a-zA-Z0-9_]+)(,([a-zA-Z0-9_]+))*$ ]]; then
echo "Invalid Column Name"
else
output=$(select_columns_by_name "$cols")
if [[ $? -ne 0 ]]; then
echo "invalid column name.."
continue
fi
selected_headers=$(echo "$output" | head -1)
col_nums=$(echo "$output" | tail -1)
read -p "Do You Want to Apply Where Condition? [y/n]: " wcond
if [[ $wcond =~ ^[Nn]$ ]]; then
echo "$selected_headers" | awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}'
echo "----------------------------------------------------------------------------"
cut -d, -f"$col_nums" "$TBName" | awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}'
elif [[ $wcond =~ ^[Yy]$ ]]; then
read -p "Please Enter Your Condition (e.g., column_name = value): " wcol
if ! [[ $wcol =~ ^[A-Za-z0-9_]+[[:space:]]+(=|!=|>=|<=|>|<)[[:space:]]+.*$ ]]; then
echo "Invalid Condition"
continue
fi
column_name=$(echo "$wcol" | cut -d' ' -f1)
operator=$(echo "$wcol" | cut -d' ' -f2)
column_value=$(echo "$wcol" | cut -d' ' -f3)
col_num=$(awk -F, -v col="$column_name" '{if ($1 == col) {print NR; exit}}' "${TBName}.meta")
if [[ -n $col_num ]]; then
echo "$selected_headers" | awk -F, '{for (i=1; i<=NF; i++) printf "%-15s", $i; print ""}'
echo "----------------------------------------------------------------------------"
awk -F, -v col="$col_num" -v op="$operator" -v val="$column_value" -v col_nums="$col_nums" '
BEGIN { split(col_nums, cols, ",") }
{
condition_met = 0;
if (op == "=" && $col == val) condition_met = 1;
else if (op == "!=" && $col != val) condition_met = 1;
else if (op == ">" && $col > val) condition_met = 1;
else if (op == "<" && $col < val) condition_met = 1;
else if (op == ">=" && $col >= val) condition_met = 1;
else if (op == "<=" && $col <= val) condition_met = 1;
if (condition_met) {
for (i in cols) printf "%-15s", $cols[i];
print "";
}
}' "$TBName"
else
echo "Error: Column '$column_name' does not exist.make sure to put >Spaces< before and after the operator."
fi
else
echo "Invalid option. Please choose [y/n]."
fi
fi
;;
"Exit")
echo "Exiting..."
break
;;
*)
echo "Invalid option. Please choose a valid selection."
;;
esac
done