aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/clump.c
blob: 18c8e5b6659619a9e8adbd739acdfdcc3a57caa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//clumps together consecutive rows containing the same first column
//to have column 2- printed after a single column 1 value.
//just try it out.
//printf "a a\na b\na c\nb a\nb b\nc a\nc b\n" | clump
//still working on the name.

int main() {
 char line[256];
 char *id;
 char *value;
 char *oldid=malloc(1);
 *oldid=0;
 while(fgets(line,sizeof(line),stdin)) {
  id=line;
  if(strchr(line,'\n')) *strchr(line,'\n')=0;
  if(strchr(id,' ')) {
   value=strchr(id,' ');
   *value=0;
   value++;
  }
  if(strcmp(id,oldid)) {
   if(*oldid != 0) {
    printf("\n");
   }
   printf("%s:",id);
   free(oldid);
   oldid=strdup(id);
  }
  printf(" %s",value);
 }
 printf("\n");
 return 0;
}