Skip to content

Instantly share code, notes, and snippets.

@valvallow
Last active December 28, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valvallow/7464825 to your computer and use it in GitHub Desktop.
Save valvallow/7464825 to your computer and use it in GitHub Desktop.
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>")
(exit))
(define (csv->list file :optional (delim #\tab))
(port->list (make-csv-reader delim)
(if file
(open-input-file file)
(current-input-port))))
(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)
#\tab
(string-ref delimiter 0)))
(csv (csv->list (and (not (null? rest))
(car rest))
delim))
(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