Skip to content

Instantly share code, notes, and snippets.

@valvallow valvallow/transpose.scm
Last active Dec 28, 2015

What would you like to do?
transpose tsv/csv command.
#!/usr/bin/env gosh
(use text.csv)
(use file.util)
(use gauche.parseopt)
(define (usage cmd)
(print "Usage: " (sys-basename cmd) " [option ...] <file or input>")
(print " h|help : Show this help")
(print " d|delimiter : default:<tab>")
(define (csv->list file :optional (delim #\tab))
(port->list (make-csv-reader delim)
(if file
(open-input-file file)
(define (transpose m)
(apply map list m))
(define (main args)
(let-args (cdr args)
((help "h|help" => (cut usage (car args)))
(delimiter "d|delimiter=s" "")
(else (opt . _)
(print "Unknown option : " opt)
(usage (car args)))
. rest)
(set-signal-handler! SIGINT (^ _ (exit)))
(let* ((delim (if (string=? "" delimiter)
(string-ref delimiter 0)))
(csv (csv->list (and (not (null? rest))
(car rest))
(writer (make-csv-writer delim)))
(for-each (pa$ writer (current-output-port))
(transpose csv)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.