Sometimes you have a bunch of .csv files, but you’d like to have one big .csv file. You can merge them in Excel, Google Sheets, or something similar, but that’s slow and error-prone. Plus, you might be dealing with something like a zip code — which starts with a zero — which is annoying to deal with in Excel.
So what’s a better solution?
The command line approach to doing this in Mac OS X and a compatible Linux/BSD system looks like this:
cat *.csv >mergedFile.csv
If that’s self-explanatory then you’re done. Enjoy.
The “cat” command is short for concatenate, or basically combine. The usage structure looks like this:
cat [options] [filenames] [-] [filenames]
That cat command is really good for reading files. You’d type in:
cat works with output redirection via the greater than sign:
cat fileName0 > fileName1 will copy the contents of file 0 to file 1.
cat is more obviously used for concatenation. This would happen in a scenario like:
cat fileName0 fileName1, which would combine the two files into the text on the screen, but not edit the files or create a file.
cat fileName0 fileName1 > fileName2 will get file 0 and file 1 and combine them in the newly created file 2.
So what about our command?
cat *.csv >mergedFile.csv will combine all the csv files in a directory and place them in a newly created file, known as mergedFile.csv.
Sal Cangeloso September 7th, 2019
Posted In: Localhost / Environment
Tags: Command Line