LinuxCommandLibrary

csvstack

Vertically concatenate multiple CSV files

SYNOPSIS

csvstack [-d DELIM] [-t] [-q QUOTECHAR] [-u] [-H] [-n NAMES] [--filenames] [--maxfilesize MAXFILESIZE] [--zero] [-h] [FILE [FILE ...]]

PARAMETERS

-d DELIM, --delimiter DELIM
    Delimiter character between columns. Default: ,

-t, --tabs
    Use tabs as delimiter instead of comma

-q QUOTECHAR, --quotechar QUOTECHAR
    Character to quote CSV fields. Default: "

-u, --no-header-row
    Do not treat first row as headers

-H, --header
    Force first row to be treated as headers

-n NAMES, --names NAMES
    Comma-separated list of column names for first row

--filenames
    Prepend column with input filenames

--maxfilesize MAXFILESIZE
    Max file size to read (bytes)

--zero
    Treat empty files as having header row

-h, --help
    Show help message

DESCRIPTION

csvstack is a utility from the csvkit suite designed to concatenate multiple CSV files horizontally, appending columns from each file to the previous ones while aligning rows by position. All input files must have exactly the same number of rows, including any header rows.

By default, it treats the first row of each file as headers and merges them into a single header row, appending suffixes like .1, .2, etc., to resolve duplicate column names across files. Data rows are then stacked column-wise. This makes it ideal for combining datasets with matching row counts but different variables, such as merging multiple CSV exports from the same query or stacking multi-part data dumps.

If files lack headers, use -u to skip header processing. The --filenames option prepends a column with each file's name, useful for tracking sources. It supports standard CSV parsing options like custom delimiters and quote characters. Output is sent to stdout, allowing piping to other tools like csvcut or csvlook.

csvstack excels in batch processing tabular data without needing programming, but positional alignment means it fails if row counts mismatch.

CAVEATS

Requires all files to have identical row counts; positional alignment only, no key-based joining. Headers from later files ignored if no duplicates.

EXAMPLES

Stack two files: csvstack file1.csv file2.csv
With filenames: csvstack --filenames *.csv
No headers: csvstack -u file1.csv file2.csv | csvlook

HISTORY

Part of csvkit, developed by Christopher Groskopf starting ~2010 as Python-based CSV tools; csvstack added early for horizontal merging. Maintained on GitHub, current version 1.2+.

SEE ALSO

csvjoin(1), csvcut(1), csvlook(1), paste(1)

Copied to clipboard