3 # Copyright © 2022 Nick Bowler
5 # Partial implementation of POSIX "join" command: only the "-v" and "-a"
6 # options are implemented.
8 # Not all awk implementations support reading from standard input with the
9 # getline function by specifying a filename of "-". In particular, busybox
10 # awk will read from a file named "-" instead of standard input. Since
11 # busybox-based environments are typically missing "join", this limitation
12 # is problematic. As a workaround, do not use "-" for the second input
15 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
16 # This is free software: you are free to do what the fuck you want to.
17 # There is NO WARRANTY, to the extent permitted by law.
24 # Process command-line options
25 for (i = 1; i < ARGC; i++) {
26 if (substr(ARGV[i], 1, 1) != "-" || ARGV[i] == "-")
29 opt = substr(ARGV[i], 2, 1);
30 if (opt == "a" || opt == "v") {
31 num = substr(ARGV[i], 3, 1);
33 # option argument must be next on command-line
44 } else if (num == 2) {
66 rhs_max_nr = rhs_nr = 0;
67 if (advance_rhs() == 0)
73 # Rewind RHS as we have duplicate common keys in LHS.
89 if (show_uniq_rhs && rhs_nr == rhs_max_nr)
92 if (advance_rhs() == 0)
95 if (show_uniq_lhs && $1 < rhs[1])
100 !rhs_eof && $1 == rhs[1] {
103 if (show_common) print_match();
105 } while (!rhs_eof && $1 == rhs[1]);
112 } while (advance_rhs() > 0);
116 function advance_rhs(raw, rc)
118 rc = getline raw < file2;
124 if (rhs_max_nr == rhs_nr++)
131 function finish_rhs(rc)
140 } while ((rc = getline) > 0);
145 function print_rhs(i)
150 for (i = 2; i in rhs; i++) {
151 printf " %s", rhs[i];
157 function print_match(i)