Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a CSV file into multiple files based on column value

Tags:

bash

csv

awk

I have CSV file which could look like this:

name1;1;11880
name2;1;260.483
name3;1;3355.82
name4;1;4179.48
name1;2;10740.4
name2;2;1868.69
name3;2;341.375
name4;2;4783.9

there could more or less rows and I need to split it into multiple .dat files each containing rows with the same value of the second column of this file. (Then I will make bar chart for each .dat file) For this case it should be two files:

data1.dat 
name1;1;11880
name2;1;260.483
name3;1;3355.82
name4;1;4179.48

data2.dat
name1;2;10740.4
name2;2;1868.69
name3;2;341.375
name4;2;4783.9

Is there any simple way of doing it with bash?

like image 519
user3616643 Avatar asked Dec 08 '22 02:12

user3616643


2 Answers

You can use awk to generate a file containing only a particular value of the second column:

awk -F ';' '($2==1){print}' data.dat > data1.dat

Just change the value in the $2== condition.

Or, if you want to do this automatically, just use:

awk -F ';' '{print > ("data"$2".dat")}' data.dat

which will output to files containing the value of the second column in the name.

like image 189
Andrzej Pronobis Avatar answered May 15 '23 04:05

Andrzej Pronobis


Try this:

while IFS=";" read -r a b c; do echo "$a;$b;$c" >> data${b}.dat; done <file
like image 31
Cyrus Avatar answered May 15 '23 04:05

Cyrus