Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: Notesfiles $Revision: 1.6.2.17 $; site ea.UUCP Path: utzoo!watmath!clyde!cbosgd!ihnp4!inuxc!pur-ee!uiucdcs!ea!jejones From: jejones@ea.UUCP Newsgroups: net.micro.6809 Subject: uniq subset Message-ID: <7300024@ea.UUCP> Date: Wed, 21-Nov-84 17:43:00 EST Article-I.D.: ea.7300024 Posted: Wed Nov 21 17:43:00 1984 Date-Received: Sun, 25-Nov-84 03:36:45 EST Lines: 121 Nf-ID: #N:ea:7300024:000:2511 Nf-From: ea!jejones Nov 21 16:43:00 1984 The following is a (simple to do, but worth doing) subset of the Unix uniq [sic] filter. It's not in shar format, because there's just one file. The contents of should be easy to reverse engineer... James Jones ------------------------------TARE HEAR------------------------------ /* * Unique -- a subset of the Unix "uniq" [sic] command * * usage: Unique [-udc] [input] * * semantics: examine the input (use the input pathname if it is * specified, else standard input) for identical adjacent lines. * Print some such lines, depending on the choice of flags: * * -u print "unique" lines (i.e. ones that don't appear next * to copies of themselves) * -d print non-"unique" lines * -c prefix lines printed with a count of the number of * (adjacent) occurrences of the line * * defaults: -ud if no flags are specified; standard input if no * input pathname is specified. * * Note that "unique" lines are really unique only if the file is * sorted. */ #include #include #define MAXLINE 256 char Buffer[2][MAXLINE]; int Count; bool Unique, Repeated, LineSpec, PrintCount; main(argc, argv) int argc; char *argv[]; { int i; char *ArgScan, *Master, *Slave, *Temp; FILE *Input; bool Done; LineSpec = FALSE; Unique = FALSE; Repeated = FALSE; PrintCount = FALSE; for (i = 1; i < argc; i++) { if (*argv[i] != '-') break; for (ArgScan = argv[i] + 1; *ArgScan; ArgScan++) { switch (*ArgScan) { case 'u': Unique = TRUE; LineSpec = TRUE; break; case 'd': Repeated = TRUE; LineSpec = TRUE; break; case 'c': PrintCount = TRUE; break; default: fprintf(stderr, "Unique: unknown option %c\n", *ArgScan); exit(1); } } } if (!LineSpec) Unique = Repeated = TRUE; if (i < argc - 1) { fputs("usage: Unique [-udc] [input]\n", stderr); exit(1); } else if (i == argc - 1) { if ((Input = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "Unique: can't open input file\n", argv[i]); exit(1); } } else Input = stdin; Master = Buffer[0]; Slave = Buffer[1]; Done = fgets(Master, MAXLINE, Input) == NULL; while (!Done) { Count = 1; for (;;) { if (Done = fgets(Slave, MAXLINE, Input) == NULL) break; if (strcmp(Master, Slave) != 0) break; Count++; } if ((Count == 1) ? Unique : Repeated) { if (PrintCount) printf("%4d ", Count); fputs(Master, stdout); } Temp = Master; Master = Slave; Slave = Temp; } fclose(Input); }