Convert Text File to CSV in Linux
Convert text to CSV on Linux with command-line tools. Use awk and sed one-liners for tab-, space- and pipe-separated input, csvkit for richer handling, and stream very large files without loading them into memory.
Convert text to CSV at the Linux command line
On Linux, a one-line command often converts text to CSV faster than opening any application, and because tools like awk stream input line by line, they handle enormous files comfortably. Below are reliable recipes for the common delimiters plus csvkit for trickier data.
Need a visual, no-install option? The online converter works in any browser. For automation and big files, the command line below is hard to beat.
awk and sed one-liners
The simplest conversion replaces the delimiter. For a tab-separated file with no quoting needed:
sed 's/\t/,/g' input.txt > output.csvawk is safer because it works with fields. Convert tab- or whitespace-separated text to comma CSV:
awk 'BEGIN { FS="\t"; OFS="," } { $1=$1 } 1' input.txt > output.csvawk '{ $1=$1; OFS="," } 1' input.txt > output.csvFor pipe-delimited input, set FS="|". The $1=$1 trick forces awk to rebuild each line using the output field separator.
Handling quotes with csvkit
Plain awk/sed do not add CSV quoting, so values containing commas can break. csvkit (install with pip install csvkit) understands real CSV rules:
# from tab-delimited text
csvformat -T input.txt > output.csv
# from a custom delimiter, e.g. pipe
csvformat -d '|' input.txt > output.csvcsvkit quotes fields correctly, so addresses and notes containing commas stay in one column.
View and verify the result
Check the columns line up using column:
column -s, -t output.csv | less -SFor very large files, every command here streams line by line, so memory use stays low regardless of file size — ideal for the large text file case.
Edge cases at the command line
The plain sed and awk recipes are perfect for clean data but have one important limitation: they do not understand CSV quoting. If a field already contains a comma, replacing tabs with commas will silently create an extra column. Whenever values might contain the output delimiter, reach for csvkit’s csvformat, which quotes correctly.
A second gotcha is whitespace. Using FS=" " in awk treats every single space as a separator, so a value with an internal space splits in two; the default field-splitting (no FS set) collapses runs of whitespace instead, which is usually what you want for column-aligned text. A third is line endings: files created on Windows carry carriage returns that show up as a stray \r at the end of the last field — strip them with sed 's/\r$//' or dos2unix before converting.
Verify the output by counting fields per line: awk -F, '{print NF}' output.csv | sort -u should print a single number if every row has the same column count.
For repeated jobs, wrap your chosen command in a small shell script or function so the delimiter, encoding handling and verification step are applied consistently every time. Combined with a glob over a directory, that turns converting a whole folder of text files into a single command, and because each tool streams its input, even a directory of very large files converts without exhausting memory.
In short, sed and awk are unbeatable for clean, fast conversions, and csvkit covers the cases where quoting matters. Because every command streams its input, the command line remains the most dependable choice for very large files and for automating a whole directory at once.
Convert Text File to CSV in Linux — FAQ
How do I convert tab-separated text to CSV in Linux?
Use awk 'BEGIN{FS="\t";OFS=","}{$1=$1}1' input.txt > output.csv, or csvkit for proper quoting.
How do I handle fields that contain commas?
Use csvkit’s csvformat, which applies CSV quoting; plain sed/awk do not quote.
Can it handle very large files?
Yes. awk, sed and csvkit stream input line by line, so memory stays low.
How do I preview the CSV?
Pipe it through column -s, -t to see aligned columns in the terminal.