-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnsplit
More file actions
executable file
·264 lines (225 loc) · 8.26 KB
/
nsplit
File metadata and controls
executable file
·264 lines (225 loc) · 8.26 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
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
263
264
#!/usr/bin/awk -f
# usage: nsplit -- [OPTIONS] [FILE ...]
#
# The '--' is required, so AWK itself doesn't read the options
#
# Split FILE (stdin if not provided) into fixed-sized pieces of PREFIXn; default
# size is 1000 lines, and default PREFIX is "out". "n" is a number, starting with
# 0.
#
# Options:
# -h, --help Display this help and exit
# -s, --start NUM Start counting at NUM instead of 0. NUM must be an integer
# -p, --prefix PRE Use PRE as the output prefix
# -l, --lines NUM Split file into pieces of NUM lines. NUM must be a positive
# integer
# -0, --pad NUM Pad suffix to NUM digits with zeroes. NUM must be a
# positive integer
## usage: getopts(optstring [, longopt_array ])
## Parses options, and deletes them from ARGV. "optstring" is of the form
## "ab:c". Each letter is a possible option. If the letter is followed by a
## colon (:), then the option requires an argument. If an argument is not
## provided, or an invalid option is given, getopts will print the appropriate
## error message and return "?". Returns each option as it's read, and -1 when
## no options are left. "optind" will be set to the index of the next
## non-option argument when finished. "optarg" will be set to the option's
## argument, when provided. If not provided, "optarg" will be empty. "optname"
## will be set to the current option, as provided. Getopts will delete each
## option and argument that it successfully reads, so awk will be able to treat
## whatever's left as filenames/assignments, as usual. If provided,
## "longopt_array" is the name of an associative array that maps long options to
## the appropriate short option (do not include the hyphens on either).
## Sample usage can be found in the examples dir, with gawk extensions, or in
## the ogrep script for a POSIX example: https://github.com/e36freak/ogrep
function getopts(optstring, longarr, opt, trimmed, hasarg, repeat) {
hasarg = repeat = 0;
optarg = "";
# increment optind
optind++;
# return -1 if the current arg is not an option or there are no args left
if (ARGV[optind] !~ /^-/ || optind >= ARGC) {
return -1;
}
# if option is "--" (end of options), delete arg and return -1
if (ARGV[optind] == "--") {
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
return -1;
}
# if the option is a long argument...
if (ARGV[optind] ~ /^--/) {
# trim hyphens
trimmed = substr(ARGV[optind], 3);
# if of the format --foo=bar, split the two. assign "bar" to optarg and
# set hasarg to 1
if (trimmed ~ /=/) {
optarg = trimmed;
sub(/=.*/, "", trimmed); sub(/^[^=]*=/, "", optarg);
hasarg = 1;
}
# invalid long opt
if (!(trimmed in longarr)) {
printf("unrecognized option -- '%s'\n", ARGV[optind]) > "/dev/stderr";
return "?";
}
opt = longarr[trimmed];
# set optname by prepending dashes to the trimmed argument
optname = "--" trimmed;
# otherwise, it is a short option
} else {
# remove the hyphen, and get just the option letter
opt = substr(ARGV[optind], 2, 1);
# set trimmed to whatevers left
trimmed = substr(ARGV[optind], 3);
# invalid option
if (!index(optstring, opt)) {
printf("invalid option -- '%s'\n", opt) > "/dev/stderr";
return "?";
}
# if there is more to the argument than just -o
if (length(trimmed)) {
# if option requires an argument, set the rest to optarg and hasarg to 1
if (index(optstring, opt ":")) {
optarg = trimmed;
hasarg = 1;
# otherwise, prepend a hyphen to the rest and set repeat to 1, so the
# same arg is processed again without the first option
} else {
ARGV[optind] = "-" trimmed;
repeat = 1;
}
}
# set optname by prepending a hypen to opt
optname = "-" opt;
}
# if the option requires an arg and hasarg is 0
if (index(optstring, opt ":") && !hasarg) {
# increment optind, check if no arguments are left
if (++optind >= ARGC) {
printf("option requires an argument -- '%s'\n", optname) > "/dev/stderr";
return "?";
}
# set optarg
optarg = ARGV[optind];
# if repeat is set, decrement optind so we process the same arg again
# mutually exclusive to needing an argument, otherwise hasarg would be set
} else if (repeat) {
optind--;
}
# delete all arguments up to this point, just to make sure
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
# return the option letter
return opt;
}
## usage: isnum(string)
## returns 1 if "string" is a valid number, otherwise 0
function isnum(str) {
# use a regex comparison because 'str == str + 0' has issues with some floats
if (str !~ /^-?[0-9.]+$/ || str ~ /\..*\./) {
return 0;
}
return 1;
}
# prints usage information
function usage() {
printf("%s\n\n%s\n\n%s\n%s\n%s\n\n",
"usage: nsplit -- [OPTIONS] [FILE ...]",
"The '--' is required, so AWK itself doesn't read the options",
"Split FILE (stdin if not provided) into fixed-sized pieces of PREFIXn; default",
"size is 1000 lines, and default PREFIX is \"out\". \"n\" is a number, starting with",
"0.") > "/dev/stderr";
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
" Options:",
" -h, --help Display this help and exit",
" -s, --start NUM Start counting at NUM instead of 0. NUM must be an integer",
" -p, --prefix PRE Use PRE as the output prefix",
" -l, --lines NUM Split file into pieces of NUM lines. NUM must be a positive",
" integer.",
" -0, --pad NUM Pad suffix to NUM digits with zeroes. NUM must be a",
" positive integer.") > "/dev/stderr";
}
BEGIN {
# initialize default variables
err = suffix = padding = 0;
prefix = "out";
lines = 1000;
outfile = "";
# map long options to the appropriate short ones
longopts["help"] = "h";
longopts["start"] = "s";
longopts["prefix"] = "p";
longopts["lines"] = "l";
longopts["pad"] = "0";
# parse the options
while ((opt = getopts("hs:p:l:0:")) != -1) {
# -h, --help
if (opt == "h") {
usage(); exit;
# -s, --start NUM
} else if (opt == "s") {
if (!isnum(optarg)) {
printf("%s is not a valid integer\n", optarg) > "/dev/stderr";
err = 1; exit;
}
suffix = optarg;
# -p, --prefix PRE
} else if (opt == "p") {
prefix = optarg;
# -l, --lines NUM
} else if (opt == "l") {
if (!isnum(optarg)) {
printf("%s is not a valid positive integer\n", optarg) > "/dev/stderr";
err = 1; exit;
}
lines = optarg;
# -0, --pad NUM
} else if (opt == "0") {
if (!isnum(optarg)) {
printf("%s is not a valid positive integer\n", optarg) > "/dev/stderr";
err = 1; exit;
}
padding = optarg;
# error
} else {
err = 1; exit;
}
}
# create initial filename
outfile = sprintf("%s%0*d", prefix, padding, suffix);
}
# write line to current output file
{
print > outfile;
}
# at every "lines"th line, close the previous output file and increment
!(NR % lines) {
close(outfile);
outfile = sprintf("%s%0*d", prefix, padding, ++suffix);
}
# close the last output file after all input is read, and exit
END {
close(outfile);
exit err;
}
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.