-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column-uniquely-sorted.awk
executable file
·263 lines (225 loc) · 8.87 KB
/
column-uniquely-sorted.awk
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/awk -f
# awk-utilities/column-uniquely-sorted script
# Copyright (C) 2021 S0AndS0
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
##
# Assigns parsed reference from ARGC matched from acceptable arguments reference
# @note - Dunder variables are function scoped, and should **not** be assigned by caller
# @note - Use `gawk -h` to list parameters that parser may conflict with argument parsing
# @parameter {ArrayReference} _acceptable_arguments - Array reference of acceptable arguments
# @parameter {ArrayReference} _parsed_arguments - Array reference that parsed arguments should be saved to
# @parameter {string} __key__ - Associative array key that points to `_acceptable_arguments[__key__]` value
# @parameter {string[]} __acceptable_parts__ - Array split by `:` from `_acceptable_arguments[__key__]` value
# @parameter {string} __pattern__ - Regexp pattern from `_acceptable_arguments[__key__]` value
# @parameter {string} __type__ - Default `"value"` from `_acceptable_arguments[__key__]` value
# @parameter {number} __index__ - Index that points to value within `ARGC`
# @parameter {string[]} __argument_parts__ - Array split by `=` from `ARGC[__index__]` value
# @parameter {string} __parameter__ - Value from `__acceptable_parts__[1]`
# @author S0AndS0
# @license AGPL-3.0
# @example
# #!/usr/bin/gawk -f
#
#
# @include "argument-parser"
#
#
# BEGIN {
# delete parsed_arguments
# delete acceptable_arguments
#
# acceptable_arguments["usage"] = "--usage:boolean"
# acceptable_arguments["key"] = "--key|-k:value"
# acceptable_arguments["increment"] = "-I:increment"
#
# argument_parser(acceptable_arguments, parsed_arguments)
#
# for (k in parsed_arguments) {
# print "parsed_arguments[\"" k "\"] ->", parsed_arguments[k]
# }
# }
function argument_parser(_acceptable_arguments, _parsed_arguments,
__key__, __acceptable_parts__, __pattern__, __type__,
__index__, __argument_parts__, __parameter__)
{
for (__index__ = 1; __index__ < ARGC; __index__++) {
for (__key__ in _acceptable_arguments) {
split(_acceptable_arguments[__key__], __acceptable_parts__, ":")
__pattern__ = __acceptable_parts__[1]
__type__ = __acceptable_parts__[2]
if (ARGV[__index__] ~ "^(" __pattern__ ")=.*$") {
split(ARGV[__index__], __argument_parts__, "=")
__parameter__ = __argument_parts__[1]
_parsed_arguments[__key__] = substr(ARGV[__index__], (length(__parameter__) + 2))
delete ARGV[__index__]
if (__type__ !~ "^(array|list)") {
delete _acceptable_arguments[__key__]
}
break
} else if (ARGV[__index__] ~ "^(" __pattern__ ")$") {
if (__type__ ~ "^bool") {
_parsed_arguments[__key__] = 1
delete ARGV[__index__]
delete _acceptable_arguments[__key__]
break
} else if (__type__ ~ "^increment") {
_parsed_arguments[__key__]++
delete ARGV[__index__]
break
} else if (__type__ ~ "^(array|list)") {
_parsed_arguments[__key__][(length(_parsed_arguments[__key__]) + 1)] = ARGV[__index__ + 1]
delete ARGV[__index__]
__index__++
delete ARGV[__index__]
break
} else {
_parsed_arguments[__key__] = ARGV[__index__ + 1]
delete ARGV[__index__]
__index__++
delete ARGV[__index__]
delete _acceptable_arguments[__key__]
break
}
}
}
}
}
##
# Print usage to Standard Out (STDOUT)
function __usage__() {
print "Collects and sorts unique columns\n\n";
print "Usage: column-uniquely-sorted.awk [OPTION]...\n\n";
print "Options:";
print "--blank <string>";
print " Blank line identifier, if undefined then blank lines/columns are ignored\n";
print "--column <number>";
print " Selected column to collect, count, and sort. Default `0`\n";
print "--count";
print " Sorts by count if defined\n";
print "--usage";
print " Prints this message and exits\n";
print "--reverse";
print " Reverse sorted output\n";
print "--version";
print " Prints version for this script and exits\n\n";
print "Examples:";
print "# file-one.txt";
print " foo";
print " bar";
print " spam";
print " ham\n";
print "# file-two.txt";
print " foo";
print " lamb";
print " spam";
print " ham\n";
print "column-uniquely-sorted.awk file-one.txt file-two.txt";
print " #> bar";
print " #> foo";
print " #> ham";
print " #> lamb";
print " #> spam\n";
print "column-uniquely-sorted.awk --count --reverse file-one.txt file-two.txt";
print " #> 2 spam";
print " #> 2 ham";
print " #> 2 foo";
print " #> 1 lamb";
print " #> 1 bar";
}
##
# Print license to Standard Out (STDOUT)
# @parameter {string} __date__
# @parameter {number} __year__
# @parameter {string} __author__
function __print_license__(__date__, __year__, __author__) {
__author__ = "S0AndS0";
__date__ = "date +'%Y'";
__date__ | getline __year__;
close(__date__);
print "column-uniquely-sorted.awk - 0.0.1\n";
print "Copyright (C)", __year__, __author__ "\n";
print "This program is free software: you can redistribute it and/or modify";
print "it under the terms of the GNU Affero General Public License as published";
print "by the Free Software Foundation, version 3 of the License.\n";
print "This program is distributed in the hope that it will be useful,";
print "but WITHOUT ANY WARRANTY; without even the implied warranty of";
print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the";
print "GNU Affero General Public License for more details.\n";
print "You should have received a copy of the GNU Affero General Public License";
print "along with this program. If not, see <https://www.gnu.org/licenses/>.";
}
##
# Initialize variables and parse command-line options
BEGIN {
delete parsed_arguments;
delete acceptable_arguments;
acceptable_arguments["blank"] = "--blank:value";
acceptable_arguments["column"] = "--column:value";
acceptable_arguments["count"] = "--count:boolean";
acceptable_arguments["usage"] = "--usage:boolean";
acceptable_arguments["reverse"] = "--reverse:boolean";
acceptable_arguments["license"] = "--license:boolean";
argument_parser(acceptable_arguments, parsed_arguments);
if (parsed_arguments["usage"]) {
__usage__();
exit 0;
}
if (parsed_arguments["license"]) {
__print_license__();
exit 0;
}
delete merged_lines_array;
delete parsed_lines_array;
}
##
# Build array with line, or column, as key and count as value
{
if (parsed_arguments["blank"]) {
if (length($parsed_arguments["column"])) {
merged_lines_array[$parsed_arguments["column"]]++;
} else {
merged_lines_array[parsed_arguments["blank"]]++;
}
} else {
merged_lines_array[$parsed_arguments["column"]]++;
}
}
##
# Parse, sort, and print array of lines
END {
__index__ = 0;
if (parsed_arguments["count"]) {
for (line in merged_lines_array) {
parsed_lines_array[__index__++] = merged_lines_array[line] " " line;
delete merged_lines_array[line];
}
} else {
for (line in merged_lines_array) {
parsed_lines_array[__index__++] = line;
delete merged_lines_array[line];
}
}
asort(parsed_lines_array);
if (parsed_arguments["reverse"]) {
for (__index__ = length(parsed_lines_array); __index__ > 0; __index__--) {
print parsed_lines_array[__index__];
delete parsed_lines_array[__index__];
}
} else {
for (__index__ in parsed_lines_array) {
print parsed_lines_array[__index__];
delete parsed_lines_array[__index__];
}
}
}