/*** DB2CSV. Filter: convert DB2 command-line SQL output to CSV format. / / Invoked as: / db2csv ofile / / Arguments: / ifile input, file from 'db2 "select ..." >ofile ' command. / ofile output, SQL output translated to CSV form. / / Purpose: / Provide simple translation from "columnar" output provided by / 'db2 "select ..." ' command, into CSV form readable by / spreadsheet tools. / / How it works: / 3rd line in ifile contains column header "bars"; blanks in that / line represent breaks between the columns. Process all other / lines to put a comma in those columns, and remove extraneous / blanks. / / Exit status: 0 / / Error Conditions: / / Known bugs: / / Home: db2csv.c / / Revision history: / */ /*: CR 12/10/00 11:39 New program. */ #include #define MAX 10000 main() { char line[MAX], head[MAX], cols[MAX]; fgets (line, MAX-1, stdin); fgets (head, MAX-1, stdin); fgets (cols, MAX-1, stdin); display (head, cols); while (fgets (line, MAX-1, stdin) != NULL) { if (line[0] == '\n') exit(0); display (line, cols); } } display (char *line, char *cols) { int i; for (i=0; cols[i]; ++i) { if (cols[i] == ' ') putchar (','); else if (line[i] != ' ') putchar (line[i]); } }