tsvkit.org
TSV Toolkit
Sample data Say hi →

Fix Ragged TSV Rows

pad · truncate · drop · reports the offending line numbers

"Row 4,812 has 7 fields, expected 6" — the error every TSV consumer eventually throws. The validator tells you where it happens; this page makes the file rectangular so it loads. Pick a target width, decide whether short rows get padded and long rows get cut, and the status line names the first offending lines so you can go and look at what actually went wrong before you paper over it.

repair
Drop a .tsv file here, or
ready

How to use

  1. Paste or drop the file. The status line immediately reports the target width and which lines don't match it.
  2. Check the width source. Header row is right when you trust row one; most common row width is right when the header itself is the broken row.
  3. Pick an action. Pad-and-truncate produces a loadable file; drop bad rows produces a clean one at the cost of losing data.
  4. Look at the named lines in the original before you accept the fix — see below for why.
  5. Copy or download.

Why the file is ragged in the first place

A tab inside a field. The most common cause and the most dangerous, because padding hides it. A free-text field containing a tab splits into two columns, so that row has one extra field and everything after the tab is shifted one column right. Padding or truncating leaves a row full of correctly-counted but wrongly-placed data. If most bad rows have exactly one extra column, this is almost certainly what happened — fix the source instead, or run the file through escape tabs and newlines after re-exporting with quoting on.

A newline inside a field. One logical record becomes two physical lines — one too short, followed by one too short. If your bad line numbers come in adjacent pairs, that's the signature. Padding both halves gives you two broken records where you had one good one.

Genuinely optional trailing columns. Plenty of tools omit trailing empty fields, so a row ending in three empty values gets written short. Here padding is exactly right and completely safe — this is the case the tool is built for.

Concatenated files with different schemas. Two exports appended with cat, one with an extra column. The header row appears twice and widths differ in blocks. Merge TSV files handles this properly by unioning headers; padding does not.

What each action does

Pad short, truncate long forces every row to the target width — the file will load anywhere afterwards. Pad short only leaves over-long rows alone, so you keep the extra data and can inspect it. Truncate long only is for the reverse case. Drop bad rows removes anything that doesn't match the target; the count of dropped rows is your data-loss figure, and if it's large the target width is probably wrong.

Padding uses an empty string unless you supply something else. A literal marker like MISSING or \N is often better than an empty cell, because it survives a round trip through a spreadsheet and shows up in stats as a distinct value rather than as a null you might not notice.

FAQ

How do I find out what's in the bad rows before fixing them?

Run the validator — it lists every offending line with its actual column count — then open the file at those lines. Slice rows will pull out a specific range if the file is too big to scroll.

Should I pad or drop?

Pad when the missing fields are genuinely absent (optional trailing columns) — nothing is lost. Drop when a row is corrupt and a half-record is worse than no record, which is usually the case for anything you'll aggregate or join on. Never pad without looking at a couple of the bad rows first.

Can it fix rows split across two lines?

No — rejoining them requires knowing where the record really ended, which the file no longer tells you. If the source can be re-exported with quoting enabled, do that; the parser here reads RFC-4180 quoting, so a quoted newline stays inside its field and the file isn't ragged at all.

What's the awk equivalent?

To find them: awk -F'\t' 'NF!=6 {print NR": "NF}' f.tsv. To pad: awk -F'\t' -v OFS='\t' '{for(i=NF+1;i<=6;i++)$i=""; NF=6; print}' f.tsv. The awk version is faster on huge files; this page is easier when you want to see what you're doing.

Privacy

100% client-side. No upload. See the privacy policy.