Skip to content

Instantly share code, notes, and snippets.

@instanceofme
Last active December 18, 2015 15:49
Show Gist options
  • Save instanceofme/5807067 to your computer and use it in GitHub Desktop.
Save instanceofme/5807067 to your computer and use it in GitHub Desktop.
csv2tsv Translate a CSV file into an unquoted TSV file (for easier handling with bash script). `$ csv2tsv file.csv > my.tsv` Requires [scala]. Supports optional double quote field protection, including `""` representing a single quote inside. Does not support (yet): optional whitespace around separator (will be kept as-is in TSV), output field q…
#!/usr/bin/env scala
//!#
import scala.language.postfixOps
import scala.io.Source
import scala.util.parsing.combinator._
/* The MIT License (MIT)
*
* Copyright (c) 2013 Adrien Lavoillotte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class FieldParser(val sep: String) extends RegexParsers {
override val skipWhitespace = false
def uField: Parser[String] = ("""(?!")[^""" + sep + "]*+").r
def eField: Parser[String] = """(?:[^"]|"")*+""".r ^^ { _.replace("\"\"", "\"") }
def pField: Parser[String] = "\"" ~> eField <~ "\""
def field: Parser[String] = pField | uField
def line: Parser[List[String]] = field ~ rep( sep ~> field ) ^^ {
case field ~ list => field :: list
}
def apply(input: String): List[String] = parseAll(line, input) match {
case Success(result, _) => result
case failure : NoSuccess => throw new IllegalArgumentException(failure.msg)
}
}
def translate(csvFile: String, sep: String = ";") {
var tabs = 0
val parse = new FieldParser(sep)
try {
for (line <- Source fromFile csvFile getLines) {
println(parse(line) mkString "\t")
tabs += line.count(_ == '\t')
}
} catch {
case e: IllegalArgumentException => System.err.println("Parsing error: " + e.getMessage)
case e: Throwable => e.printStackTrace()
}
if (tabs > 0) {
System.err.println("WARN: " + tabs +
" tab characters were found in the CSV file and were NOT escaped in the resulting output.")
}
}
args match {
case Array(csv, sep, _*) if !sep.isEmpty => translate(csv, sep)
case Array(csv) => translate(csv)
case _ => System.err.println(
"Usage: ./csv2tsv file.csv [sep]\n" +
"Translates CSV file into TSV file\n" +
"Default separator is ';'")
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
A1;A2;A3
"B1";"B2";"B3"
"""C1";"C""2";"C3"""
"D\1";"D\""2";"D3\"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment