csv-shape-check v1.0.0

Crystal CLI and shard: check the shape of a CSV before an import: BOM, line endings, delimiter, header problems, uneven rows, stray spaces. Offline.

csv-shape-check

Crystal command line tool and library. Check the shape of a CSV file before you import it. It uses its own small CSV reader that understands double quotes, "" escapes and line breaks inside quoted cells. It reports: bytes and records read (the header is record 1); whether a UTF-8 byte order mark is present; the line ending style (LF, CRLF, CR or mixed, counted outside quotes only); a delimiter guess among comma, semicolon, tab and pipe (the one whose field count is most consistent over the first 20 records, ties going in that order, and only counted when its most common field count is above one; a file that fits none is treated as a single column with a comma); empty header names and duplicate header names (trimmed, case-insensitive, with their column numbers); records whose field count differs from the header (the count, and the first 10 with record number, line and field count); blank lines; fully empty records (every field empty); cells with a leading or trailing space (the count, and the first 5 as record and column); and an unterminated quote at the end of the file, named by the line where the quote opened. Counted as a problem, and so exit 1: a byte order mark, mixed line endings, a guessed delimiter that is not the comma, and every item above that is not zero. CRLF on its own is not a problem. The tool looks at shape only: it does not judge what the cells contain, does not check emails or dates, and does not change the file. Records are numbered without the blank lines; lines are physical lines. A record that is still open when the file ends because of an unterminated quote is not counted in the records read. A quote opens a quoted cell only at the very start of a cell; a quote in the middle of a cell is read as an ordinary character. A space means the space character only, not a tab.

Part of the OneGTMLab free GTM tools. Background on the idea: verify emails.

Install

shards build   # then ./bin/csv-shape-check

Use

Input: any CSV file whose first record is the header. Option: --delimiter "," (or ";", "tab", "|") skips the guess and uses that delimiter, which is then not counted as a problem. Exit 0 when no problem is found, 1 when any problem is found, 2 when the file cannot be read (missing file, a folder, an empty file, a file with only blank lines), the delimiter value is bad or an option is unknown. The sample file is small and holds each problem once: it starts with a byte order mark, header line 1 ends in CRLF while most others end in LF, the header has an empty name at column 3 and "name" and "Name" twice, line 4 is blank, line 5 has 4 fields and line 9 has 6, line 6 is an empty record, one quoted cell holds a line break (lines 7 and 8, not counted as an ending), three cells have stray spaces, and the quote opened on line 11 is never closed. The command below ends in || true only so the shell does not stop on the exit code 1 the tool returns when it finds problems.

$ bin/csv-shape-check sample.csv || true
csv-shape-check: sample.csv
bytes: 216
records read: 8 (the header is record 1)
UTF-8 byte order mark: yes
line endings: mixed (LF 7, CRLF 2)
delimiter: comma (guessed), is the comma: yes
header columns: 5
empty header names: at column 3
duplicate header names: "name" at columns 2, 4
records with a different field count than the header: 2
  record 4 (line 5): 4 fields, header has 5
  record 7 (line 9): 6 fields, header has 5
blank lines: 1
fully empty records: 1
cells with leading or trailing spaces: 3
  record 2, column 5
  record 3, column 2
  record 8, column 2
unterminated quote: the quote opened on line 11 is never closed

result: 9 problems found (byte order mark present, mixed line endings, empty header names, duplicate header names, records with a different field count, blank lines, fully empty records, cells with leading or trailing spaces, unterminated quote)

Test

crystal spec

Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.

Repository

csv-shape-check

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 1 hour ago
  • November 29, 2026
License

MIT License

Links
Synced at

Sun, 29 Nov 2026 06:15:49 GMT

Languages