user3616643
user3616643

Reputation: 153

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

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?

Upvotes: 5

Views: 10398

Answers (2)

Andrzej Pronobis
Andrzej Pronobis

Reputation: 36086

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.

Upvotes: 13

Cyrus
Cyrus

Reputation: 88563

Try this:

while IFS=";" read -r a b c; do echo "$a;$b;$c" >> data${b}.dat; done <file

Upvotes: 2

Related Questions