Last active
December 10, 2015 06:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$5 != "" && $16 != "" { | |
print $5 "," $16 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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