Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest method for splitting CSV file based on column value

I'm have a large list of every city in the world that I'm attempting to break down by country. Making this happen manually has been very tedious leading be to believe there is a better way to accomplish this. Which bring me to the follwing question.

What is the simplest/best way to split a CSV file into smaller CSV files based on similar values in one column?

I have seen this question answered a couple different ways assuming the person asking had experience in C++, which I do not. I have a very basic knowledge of Java, a bit more experience with Linux, and even more with Windows and Microsoft Office.

Any push in the right direction is helpful.

Thanks for taking the time to read this far. Any and all help is appreciated.

like image 735
contact411 Avatar asked Mar 20 '26 09:03

contact411


1 Answers

There are obviously many different ways you could accomplish this - if you're on Windows, this snippet provides a nice one-liner for PowerShell to split a csv file based on the values in a column:

Import-Csv file.csv | Group-Object -Property "department" | 
    Foreach-Object {$path=$_.name+".csv" ; $_.group | 
    Export-Csv -Path $path -NoTypeInformation}

Replace file.csv with your csv file, and "department" with the column heading you want to break up files on. It will write a new csv based on each unique value in the selected column.

If you've never used Powershell, here is a decent introduction.

like image 132
chrisb Avatar answered Mar 23 '26 02:03

chrisb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!