| 1 |
# split.awk --- do split in awk |
| 2 |
# |
| 3 |
# Requires getopt() library function. |
| 4 |
# |
| 5 |
# Arnold Robbins, [email protected], Public Domain |
| 6 |
# May 1993 |
| 7 |
# Revised slightly, May 2014 |
| 8 |
# Rewritten September 2020 |
| 9 |
|
| 10 |
|
| 11 |
function usage( common) |
| 12 |
{ |
| 13 |
common = "[-a suffix-len] [file [outname]]" |
| 14 |
printf("usage: split [-l count] %s\n", common) > "/dev/stderr" |
| 15 |
printf(" split [-b N[k|m]] %s\n", common) > "/dev/stderr" |
| 16 |
exit 1 |
| 17 |
} |
| 18 |
BEGIN { |
| 19 |
# Set defaults: |
| 20 |
Suffix_length = 2 |
| 21 |
Line_count = 1000 |
| 22 |
Byte_count = 0 |
| 23 |
Outfile = "x" |
| 24 |
|
| 25 |
parse_arguments() |
| 26 |
|
| 27 |
init_suffix_data() |
| 28 |
|
| 29 |
Output = (Outfile compute_suffix()) |
| 30 |
} |
| 31 |
function parse_arguments( i, c, l, modifier) |
| 32 |
{ |
| 33 |
while ((c = getopt(ARGC, ARGV, "a:b:l:")) != -1) { |
| 34 |
if (c == "a") |
| 35 |
Suffix_length = Optarg + 0 |
| 36 |
else if (c == "b") { |
| 37 |
Byte_count = Optarg + 0 |
| 38 |
Line_count = 0 |
| 39 |
|
| 40 |
l = length(Optarg) |
| 41 |
modifier = substr(Optarg, l, 1) |
| 42 |
if (modifier == "k") |
| 43 |
Byte_count *= 1024 |
| 44 |
else if (modifier == "m") |
| 45 |
Byte_count *= 1024 * 1024 |
| 46 |
} else if (c == "l") { |
| 47 |
Line_count = Optarg + 0 |
| 48 |
Byte_count = 0 |
| 49 |
} else |
| 50 |
usage() |
| 51 |
} |
| 52 |
|
| 53 |
# Clear out options |
| 54 |
for (i = 1; i < Optind; i++) |
| 55 |
ARGV[i] = "" |
| 56 |
|
| 57 |
# Check for filename |
| 58 |
if (ARGV[Optind]) { |
| 59 |
Optind++ |
| 60 |
|
| 61 |
# Check for different prefix |
| 62 |
if (ARGV[Optind]) { |
| 63 |
Outfile = ARGV[Optind] |
| 64 |
ARGV[Optind] = "" |
| 65 |
|
| 66 |
if (++Optind < ARGC) |
| 67 |
usage() |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
function compute_suffix( i, result, letters) |
| 72 |
{ |
| 73 |
# Logical step 3 |
| 74 |
if (Reached_last) { |
| 75 |
printf("split: too many files!\n") > "/dev/stderr" |
| 76 |
exit 1 |
| 77 |
} else if (on_last_file()) |
| 78 |
Reached_last = 1 # fail when wrapping after 'zzz' |
| 79 |
|
| 80 |
# Logical step 1 |
| 81 |
result = "" |
| 82 |
letters = "abcdefghijklmnopqrstuvwxyz" |
| 83 |
for (i = 1; i <= Suffix_length; i++) |
| 84 |
result = result substr(letters, Suffix_ind[i], 1) |
| 85 |
|
| 86 |
# Logical step 2 |
| 87 |
for (i = Suffix_length; i >= 1; i--) { |
| 88 |
if (++Suffix_ind[i] > 26) { |
| 89 |
Suffix_ind[i] = 1 |
| 90 |
} else |
| 91 |
break |
| 92 |
} |
| 93 |
|
| 94 |
return result |
| 95 |
} |
| 96 |
function init_suffix_data( i) |
| 97 |
{ |
| 98 |
for (i = 1; i <= Suffix_length; i++) |
| 99 |
Suffix_ind[i] = 1 |
| 100 |
|
| 101 |
Reached_last = 0 |
| 102 |
} |
| 103 |
function on_last_file( i, on_last) |
| 104 |
{ |
| 105 |
on_last = 1 |
| 106 |
for (i = 1; i <= Suffix_length; i++) { |
| 107 |
on_last = on_last && (Suffix_ind[i] == 26) |
| 108 |
} |
| 109 |
|
| 110 |
return on_last |
| 111 |
} |
| 112 |
Line_count > 0 { |
| 113 |
if (++tcount > Line_count) { |
| 114 |
close(Output) |
| 115 |
Output = (Outfile compute_suffix()) |
| 116 |
tcount = 1 |
| 117 |
} |
| 118 |
print > Output |
| 119 |
} |
| 120 |
Byte_count > 0 { |
| 121 |
# `+ 1' is for the final newline |
| 122 |
if (tcount + length($0) + 1 > Byte_count) { # would overflow |
| 123 |
# compute leading bytes |
| 124 |
leading_bytes = Byte_count - tcount |
| 125 |
|
| 126 |
# write leading bytes |
| 127 |
printf("%s", substr($0, 1, leading_bytes)) > Output |
| 128 |
|
| 129 |
# close old file, open new file |
| 130 |
close(Output) |
| 131 |
Output = (Outfile compute_suffix()) |
| 132 |
|
| 133 |
# set up first bytes for new file |
| 134 |
$0 = substr($0, leading_bytes + 1) # trailing bytes |
| 135 |
tcount = 0 |
| 136 |
} |
| 137 |
|
| 138 |
# write full record or trailing bytes |
| 139 |
tcount += length($0) + 1 |
| 140 |
print > Output |
| 141 |
} |
| 142 |
END { |
| 143 |
close(Output) |
| 144 |
} |