| 1 |
# cut.awk --- implement cut in awk |
| 2 |
# |
| 3 |
# Arnold Robbins, [email protected], Public Domain |
| 4 |
# May 1993 |
| 5 |
|
| 6 |
# Options: |
| 7 |
# -c list Cut characters |
| 8 |
# -f list Cut fields |
| 9 |
# -d c Field delimiter character |
| 10 |
# |
| 11 |
# -s Suppress lines without the delimiter |
| 12 |
# |
| 13 |
# Requires getopt() and join() library functions |
| 14 |
|
| 15 |
function usage() |
| 16 |
{ |
| 17 |
print("usage: cut [-f list] [-d c] [-s] [files...]") > "/dev/stderr" |
| 18 |
print(" cut [-c list] [files...]") > "/dev/stderr" |
| 19 |
exit 1 |
| 20 |
} |
| 21 |
BEGIN { |
| 22 |
FS = "\t" # default |
| 23 |
OFS = FS |
| 24 |
while ((c = getopt(ARGC, ARGV, "sf:c:d:")) != -1) { |
| 25 |
if (c == "f") { |
| 26 |
by_fields = 1 |
| 27 |
fieldlist = Optarg |
| 28 |
} else if (c == "c") { |
| 29 |
by_chars = 1 |
| 30 |
fieldlist = Optarg |
| 31 |
OFS = "" |
| 32 |
} else if (c == "d") { |
| 33 |
if (length(Optarg) > 1) { |
| 34 |
printf("cut: using first character of %s" \ |
| 35 |
" for delimiter\n", Optarg) > "/dev/stderr" |
| 36 |
Optarg = substr(Optarg, 1, 1) |
| 37 |
} |
| 38 |
fs = FS = Optarg |
| 39 |
OFS = FS |
| 40 |
if (FS == " ") # defeat awk semantics |
| 41 |
FS = "[ ]" |
| 42 |
} else if (c == "s") |
| 43 |
suppress = 1 |
| 44 |
else |
| 45 |
usage() |
| 46 |
} |
| 47 |
|
| 48 |
# Clear out options |
| 49 |
for (i = 1; i < Optind; i++) |
| 50 |
ARGV[i] = "" |
| 51 |
if (by_fields && by_chars) |
| 52 |
usage() |
| 53 |
|
| 54 |
if (by_fields == 0 && by_chars == 0) |
| 55 |
by_fields = 1 # default |
| 56 |
|
| 57 |
if (fieldlist == "") { |
| 58 |
print "cut: needs list for -c or -f" > "/dev/stderr" |
| 59 |
exit 1 |
| 60 |
} |
| 61 |
|
| 62 |
if (by_fields) |
| 63 |
set_fieldlist() |
| 64 |
else |
| 65 |
set_charlist() |
| 66 |
} |
| 67 |
function set_fieldlist( n, m, i, j, k, f, g) |
| 68 |
{ |
| 69 |
n = split(fieldlist, f, ",") |
| 70 |
j = 1 # index in flist |
| 71 |
for (i = 1; i <= n; i++) { |
| 72 |
if (index(f[i], "-") != 0) { # a range |
| 73 |
m = split(f[i], g, "-") |
| 74 |
if (m != 2 || g[1] >= g[2]) { |
| 75 |
printf("cut: bad field list: %s\n", |
| 76 |
f[i]) > "/dev/stderr" |
| 77 |
exit 1 |
| 78 |
} |
| 79 |
for (k = g[1]; k <= g[2]; k++) |
| 80 |
flist[j++] = k |
| 81 |
} else |
| 82 |
flist[j++] = f[i] |
| 83 |
} |
| 84 |
nfields = j - 1 |
| 85 |
} |
| 86 |
function set_charlist( field, i, j, f, g, n, m, t, |
| 87 |
filler, last, len) |
| 88 |
{ |
| 89 |
field = 1 # count total fields |
| 90 |
n = split(fieldlist, f, ",") |
| 91 |
j = 1 # index in flist |
| 92 |
for (i = 1; i <= n; i++) { |
| 93 |
if (index(f[i], "-") != 0) { # range |
| 94 |
m = split(f[i], g, "-") |
| 95 |
if (m != 2 || g[1] >= g[2]) { |
| 96 |
printf("cut: bad character list: %s\n", |
| 97 |
f[i]) > "/dev/stderr" |
| 98 |
exit 1 |
| 99 |
} |
| 100 |
len = g[2] - g[1] + 1 |
| 101 |
if (g[1] > 1) # compute length of filler |
| 102 |
filler = g[1] - last - 1 |
| 103 |
else |
| 104 |
filler = 0 |
| 105 |
if (filler) |
| 106 |
t[field++] = filler |
| 107 |
t[field++] = len # length of field |
| 108 |
last = g[2] |
| 109 |
flist[j++] = field - 1 |
| 110 |
} else { |
| 111 |
if (f[i] > 1) |
| 112 |
filler = f[i] - last - 1 |
| 113 |
else |
| 114 |
filler = 0 |
| 115 |
if (filler) |
| 116 |
t[field++] = filler |
| 117 |
t[field++] = 1 |
| 118 |
last = f[i] |
| 119 |
flist[j++] = field - 1 |
| 120 |
} |
| 121 |
} |
| 122 |
FIELDWIDTHS = join(t, 1, field - 1) |
| 123 |
nfields = j - 1 |
| 124 |
} |
| 125 |
{ |
| 126 |
if (by_fields && suppress && index($0, fs) == 0) |
| 127 |
next |
| 128 |
|
| 129 |
for (i = 1; i <= nfields; i++) { |
| 130 |
if ($flist[i] != "") { |
| 131 |
printf "%s", $flist[i] |
| 132 |
if (i < nfields && $flist[i+1] != "") |
| 133 |
printf "%s", OFS |
| 134 |
} |
| 135 |
} |
| 136 |
print "" |
| 137 |
} |