Skip to content

Instantly share code, notes, and snippets.

@mhkeller
Created October 22, 2018 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhkeller/0953799f5673cb205fbb6ff95581d306 to your computer and use it in GitHub Desktop.
Save mhkeller/0953799f5673cb205fbb6ff95581d306 to your computer and use it in GitHub Desktop.
Concatenate csv files
# Adapted from https://unix.stackexchange.com/questions/60577/concatenate-multiple-files-with-same-header#170692
# `head -1` gets first line of the file of the file and stashes them as the header row into `all.txt`
# `tail -n +2 -q *.csv >> all.txt` grabs every csv file from the second row down and stashes them into `all.txt`
# `all.txt` is a csv file but we use the txt extension to avoid it being captured in the `*.csv` glob.
# You could also output to a csv file by having your input files share a naming convention such as `file-001.csv` and glob on `file*.csv`
# But renaming `all.txt` to `all.csv` is sometimes easier than worrying about a naming convention and txt and csvs are the same thing
head -1 my-csv-01.csv > all.txt; tail -n +2 -q *.csv >> all.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment