Skip to content

Instantly share code, notes, and snippets.

@codedot
Last active December 10, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codedot/4397418 to your computer and use it in GitHub Desktop.
Save codedot/4397418 to your computer and use it in GitHub Desktop.
Strictly Conforming XSI Application that counts unique non-empty keys from standard input, and prints those appearing at least MINCNT times.
#define _XOPEN_SOURCE 700
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#define NEL (2 << 20)
#define MINCNT 5
main()
{
static long nkeys, i;
static struct key {
char *key;
long cnt;
} keys[NEL];
hcreate(NEL);
while (!feof(stdin)) {
ENTRY item, *found;
char *key = NULL;
ssize_t len = 0;
struct key *uniq;
getline(&key, &len, stdin);
item.key = key;
found = hsearch(item, FIND);
if (found) {
uniq = found->data;
uniq->cnt++;
free(key);
} else {
uniq = &keys[nkeys];
uniq->key = key;
uniq->cnt++;
++nkeys;
item.data = uniq;
hsearch(item, ENTER);
}
}
for (i = 0; i < nkeys; i++) {
struct key *uniq = &keys[i];
long cnt = uniq->cnt;
if (MINCNT <= cnt)
printf("%ld\t%s", cnt, uniq->key);
}
return 0;
}
$5 != "" && $16 != "" {
print $5 "," $16
}
SCRIPT = "awk -F , -f cut.awk <input.csv | ./count >output.txt"
.POSIX:
all: cut.awk count input.csv
time sh -c $(SCRIPT)
clean:
-rm -f count output.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment