| 1 |
# uniq.awk --- do uniq in awk |
| 2 |
# |
| 3 |
# Requires getopt() and join() library functions |
| 4 |
# |
| 5 |
# Arnold Robbins, [email protected], Public Domain |
| 6 |
# May 1993 |
| 7 |
# Updated August 2020 to current POSIX |
| 8 |
|
| 9 |
function usage() |
| 10 |
{ |
| 11 |
print("Usage: uniq [-udc [-f fields] [-s chars]] " \ |
| 12 |
"[ in [ out ]]") > "/dev/stderr" |
| 13 |
exit 1 |
| 14 |
} |
| 15 |
|
| 16 |
# -c count lines. overrides -d and -u |
| 17 |
# -d only repeated lines |
| 18 |
# -u only nonrepeated lines |
| 19 |
# -f n skip n fields |
| 20 |
# -s n skip n characters, skip fields first |
| 21 |
# As of 2020, '+' can be used as the option character in addition to '-' |
| 22 |
# Previously allowed use of -N to skip fields and +N to skip |
| 23 |
# characters is no longer allowed, and not supported by this version. |
| 24 |
|
| 25 |
BEGIN { |
| 26 |
# Convert + to - so getopt can handle things |
| 27 |
for (i = 1; i < ARGC; i++) { |
| 28 |
first = substr(ARGV[i], 1, 1) |
| 29 |
if (ARGV[i] == "--" || (first != "-" && first != "+")) |
| 30 |
break |
| 31 |
else if (first == "+") |
| 32 |
# Replace "+" with "-" |
| 33 |
ARGV[i] = "-" substr(ARGV[i], 2) |
| 34 |
} |
| 35 |
} |
| 36 |
BEGIN { |
| 37 |
count = 1 |
| 38 |
outputfile = "/dev/stdout" |
| 39 |
opts = "udcf:s:" |
| 40 |
while ((c = getopt(ARGC, ARGV, opts)) != -1) { |
| 41 |
if (c == "u") |
| 42 |
non_repeated_only++ |
| 43 |
else if (c == "d") |
| 44 |
repeated_only++ |
| 45 |
else if (c == "c") |
| 46 |
do_count++ |
| 47 |
else if (c == "f") |
| 48 |
fcount = Optarg + 0 |
| 49 |
else if (c == "s") |
| 50 |
charcount = Optarg + 0 |
| 51 |
else |
| 52 |
usage() |
| 53 |
} |
| 54 |
|
| 55 |
for (i = 1; i < Optind; i++) |
| 56 |
ARGV[i] = "" |
| 57 |
|
| 58 |
if (repeated_only == 0 && non_repeated_only == 0) |
| 59 |
repeated_only = non_repeated_only = 1 |
| 60 |
|
| 61 |
if (ARGC - Optind == 2) { |
| 62 |
outputfile = ARGV[ARGC - 1] |
| 63 |
ARGV[ARGC - 1] = "" |
| 64 |
} |
| 65 |
} |
| 66 |
function are_equal( n, m, clast, cline, alast, aline) |
| 67 |
{ |
| 68 |
if (fcount == 0 && charcount == 0) |
| 69 |
return (last == $0) |
| 70 |
|
| 71 |
if (fcount > 0) { |
| 72 |
n = split(last, alast) |
| 73 |
m = split($0, aline) |
| 74 |
clast = join(alast, fcount+1, n) |
| 75 |
cline = join(aline, fcount+1, m) |
| 76 |
} else { |
| 77 |
clast = last |
| 78 |
cline = $0 |
| 79 |
} |
| 80 |
if (charcount) { |
| 81 |
clast = substr(clast, charcount + 1) |
| 82 |
cline = substr(cline, charcount + 1) |
| 83 |
} |
| 84 |
|
| 85 |
return (clast == cline) |
| 86 |
} |
| 87 |
NR == 1 { |
| 88 |
last = $0 |
| 89 |
next |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
equal = are_equal() |
| 94 |
|
| 95 |
if (do_count) { # overrides -d and -u |
| 96 |
if (equal) |
| 97 |
count++ |
| 98 |
else { |
| 99 |
printf("%4d %s\n", count, last) > outputfile |
| 100 |
last = $0 |
| 101 |
count = 1 # reset |
| 102 |
} |
| 103 |
next |
| 104 |
} |
| 105 |
|
| 106 |
if (equal) |
| 107 |
count++ |
| 108 |
else { |
| 109 |
if ((repeated_only && count > 1) || |
| 110 |
(non_repeated_only && count == 1)) |
| 111 |
print last > outputfile |
| 112 |
last = $0 |
| 113 |
count = 1 |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
END { |
| 118 |
if (do_count) |
| 119 |
printf("%4d %s\n", count, last) > outputfile |
| 120 |
else if ((repeated_only && count > 1) || |
| 121 |
(non_repeated_only && count == 1)) |
| 122 |
print last > outputfile |
| 123 |
close(outputfile) |
| 124 |
} |