Forked from mvitaly/Fixed-length-fields.txt.groovy
Created
February 16, 2018 00:52
-
-
Save martins1930/94d329fa5cd4bb2e0f3b7f8082bf262a to your computer and use it in GitHub Desktop.
DataGrip extractor for txt file with fixed length columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SEPARATOR = "|" | |
SPACE = " " | |
CORNER = "+" | |
LINE = "-" | |
NEWLINE = System.getProperty("line.separator") | |
def record(values, valueToString) { | |
values.collect { value -> | |
valueToString(value) | |
} | |
} | |
def outRecord(columnLengths, row, separator=SEPARATOR, padding=SPACE) { | |
OUT.append(separator) | |
[columnLengths, row].transpose().each { size, value -> | |
OUT.append(padding + value.padRight(size, padding) + padding + separator) | |
}; | |
OUT.append(NEWLINE) | |
} | |
COLUMN_NAMES = COLUMNS.collect { col -> col.name() } | |
def formattedRows = ROWS.collect { row -> record(COLUMNS, { FORMATTER.format(row, it) }) } | |
def columnLengths = formattedRows.inject(COLUMN_NAMES.collect { col -> col.size() }, { columnLengths, row -> | |
[columnLengths, row.collect { it.length() }].transpose().collect { collectedSize, size -> Math.max(collectedSize, size) } | |
}) | |
// OUTPUT | |
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE) | |
outRecord(columnLengths, COLUMN_NAMES) | |
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE) | |
formattedRows.each { row -> outRecord(columnLengths, row) } | |
outRecord(columnLengths, COLUMNS.collect { '' }, CORNER, LINE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment