Process CSV from the command line
A practical review of command-line CSV tools: what each transformation changes, where naive commands break, and how to keep converted data readable and usable.
Processing CSV in a terminal can mean selecting columns, filtering rows, sorting, joining files, or converting between comma- and tab-separated formats. The commands that do this fall into two groups: general text tools such as cut, awk, sort and tac, which see each line as a string, and CSV-aware tools such as csvkit, Miller, xsv or qsv, and the sqlite3 shell, which parse quoting and treat each record as a set of fields. This review explains what each kind of transformation changes and when the output stays reliable enough to share or import elsewhere.
Is command-line CSV processing suitable for your task?
For repeatable work on files you control, command-line processing is a strong fit. The main benefit is that every step is an explicit, rerunnable command, so a cleanup or conversion can be checked, scripted and applied again to next month's export without manual clicking. Installed tools also read and write files on your own machine, which avoids uploading data to a web converter whose handling you would otherwise need to verify.
The decisive tradeoff is parsing. Line-based tools are fast and already installed on most Unix-like systems, but they do not understand CSV quoting, so they silently produce wrong results when a field contains a comma, a quote character or a line break. CSV-aware tools handle those cases but must be installed and learned. If you cannot guarantee that your data is free of quoted fields, use a CSV-aware tool.
Leaving the file as plain, unmodified CSV is often the better choice when the recipient only needs to open it in a spreadsheet or database that already imports CSV correctly, when you need a few values once rather than a repeatable process, or when a conversion such as CSV to TSV would introduce escaping problems the destination cannot handle. Converting is worthwhile only when the destination actually requires a different structure or delimiter.
What a CSV transformation actually changes
Every operation below changes stored characters in the output file, not just how the data looks on screen. That distinction matters because some commands also offer display-only modes, such as aligned previews, that are meant for reading and should not be saved as data.
Column selection, filtering and sorting
Selecting columns removes fields from each record; filtering removes whole records; sorting reorders records. A CSV-aware tool keeps the header row in place and re-applies quoting only where a field needs it. A line-based tool splits on every delimiter character, including ones inside quotes, so the column numbers drift for any affected row.
# Input row: 1,"Smith, Jane",CA
cut -d, -f2 people.csv
# prints: "Smith <- wrong: the quoted comma was treated as a separator
csvcut -c 2 people.csv
# prints: "Smith, Jane" <- correct: the quoted field is kept whole
Common CSV-aware equivalents for everyday tasks look like this. Column names are used where the tool supports them, which makes scripts easier to read than numeric positions.
# csvkit
csvcut -c name,email people.csv
csvgrep -c state -m CA people.csv
csvsort -c signup_date people.csv
# Miller
mlr --csv cut -f name,email people.csv
mlr --csv filter '$state == "CA"' people.csv
mlr --csv sort -f name people.csv
# sqlite3 shell (the --csv option on .import requires a reasonably recent sqlite3)
sqlite3 -header -csv :memory: \
-cmd '.import --csv people.csv t' \
'SELECT state, COUNT(*) FROM t GROUP BY state;'
Delimiter conversion between CSV and TSV
Converting CSV to TSV replaces the field separator with a tab and usually drops the quoting that CSV needed for embedded commas. The difficulty is fields that contain tabs or line breaks: TSV has no single escaping convention that every program agrees on. Depending on the tool and its options, such fields may be quoted, escaped with sequences such as a backslash followed by t or n, or rejected. Check how your converter behaves and how the destination program expects TSV before relying on the result.
# CSV to TSV
csvformat -T people.csv > people.tsv
mlr --icsv --otsv cat people.csv > people.tsv
# TSV to CSV
csvformat -t people.tsv > people.csv
mlr --itsv --ocsv cat people.tsv > people.csv
A naive substitution such as replacing every comma with a tab using sed or tr has the same flaw as cut: commas inside quoted fields become tabs, and the leftover quote characters remain in the output.
Encoding, line endings and byte order marks
Some files change at the byte level even when the visible content does not. Exports from spreadsheet software may begin with a UTF-8 byte order mark, which a tool that does not expect it can treat as part of the first column name. The CSV specification in RFC 4180 uses CRLF line endings, while many Unix tools write LF only; most modern readers accept either, but strict importers may not. Files saved in a legacy encoding such as Windows-1252 can show garbled accented characters when read as UTF-8. These are worth checking with a command such as file or a hex viewer before and after processing.
What "reverse" means for CSV
The word is ambiguous at the command line. The tac command reverses the order of lines, which moves the header row to the bottom and splits apart any record whose quoted field spans several lines. The rev command reverses the characters within each line, which turns CSV into unusable text; it may also separate combining accent marks from their base letters or break multi-code-point emoji, depending on the implementation and locale. If the goal is to reverse record order while keeping the header first and multi-line fields intact, use a CSV-aware verb instead.
# Reverse record order, header stays on top
mlr --csv tac people.csv
Previews versus data
Commands such as csvlook and Miller's pprint output mode pad columns with spaces or draw borders so a table is easy to scan in a terminal. That is a presentation format. Saving it and sending it as if it were CSV produces a file that other programs cannot reliably parse back into fields.
csvlook people.csv
mlr --icsv --opprint cat people.csv
Comparing the main tool types
| Tool type | Understands CSV quoting | Typical strengths | Main limitation |
| cut, awk -F, sed, tr | No | Already installed; fast on simple, unquoted data | Wrong results when fields contain delimiters, quotes or line breaks |
| csvkit (Python) | Yes | Many single-purpose commands; reads Excel and JSON via in2csv | Requires a Python install |
| Miller (mlr) | Yes | Converts among CSV, TSV, JSON and other formats; filtering and reshaping verbs | Its own expression language to learn |
| xsv / qsv | Yes | Designed for speed on large files; indexing | Separate binary to install; command sets differ between the two |
| sqlite3 shell | Yes, on import | Full SQL for grouping and joins | Imports into a table first; column types need attention |
No benchmark data was available for this review, so no claims are made about which tool is fastest on a given file size. If performance matters, time each candidate on a representative sample of your own data.
Will the output stay usable after copying or sharing?
Properly quoted CSV or TSV produced by a CSV-aware tool is plain text, so it copies and pastes without hidden formatting. Usability problems tend to appear at the destination rather than in the file itself.
- Spreadsheet import: spreadsheet programs may reinterpret values when opening a CSV, for example dropping leading zeros from postal codes or IDs, showing long numbers in scientific notation, or converting text that looks like a date. Importing with columns set to text avoids this, but the behavior varies by program and version.
- Formula injection: a field beginning with =, +, - or @ may be treated as a formula when the file is opened in a spreadsheet. If you share CSV built from untrusted input, consider how the recipient will open it.
- Pasting into chat or email: pasted CSV usually arrives as raw comma-separated lines; whether it renders as a table depends entirely on the destination.
- Search and editing: CSV and TSV remain searchable with grep and editable in any text editor, but a padded preview or a bordered table is harder to edit without breaking alignment.
- Reading order: a data file has no visual reading order beyond row then column, so the main risk is a broken header or shifted columns from a naive command, not reordering by the format itself.
Accessibility depends on how the data is presented. Raw CSV in a text editor is a continuous string of values and commas, which gives a screen reader user no table structure to navigate. The same data opened in a spreadsheet or rendered as an HTML table with marked header cells generally offers row and column navigation, but no screen reader was tested for this review, so behavior with a specific assistive technology should be confirmed directly. Aligned terminal previews add padding characters and borders that may be read aloud as noise. When the audience includes assistive technology users, share the CSV for import alongside a properly structured table, rather than a terminal preview.
A safe workflow for command-line CSV changes
- Keep the original file untouched and write results to a new file.
- Check encoding, line endings and any byte order mark before processing.
- Use a CSV-aware tool unless you have confirmed the data contains no quoted fields.
- Preview a sample with csvlook or a pprint mode, but save only real CSV or TSV.
- Compare record counts before and after, for example with csvstat or mlr count, to catch rows lost or split by parsing errors.
- Open the result in the actual destination program to confirm columns, leading zeros and special characters survived.
Gaps in this review
No subject-specific evidence such as test files, timings, version-by-version behavior or destination-program tests was supplied. The descriptions above rely on documented behavior and general CSV principles. Exact options, escaping rules and import behavior can differ between tool versions and spreadsheet programs, so verify them against the documentation and a small test file before running a transformation on important data.