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

I have a CSV file that might 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 may be more or less lines and I need to split it into multiple .dat files, each containing lines with the same value of the second column of that file. (Then I'll make a histogram for each .dat file). In 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 an easy way to do this using bash?

+3


source to share


2 answers


You can use awk to create a file containing only the specific value of the second column:

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

      

Just change the value in $2==

.



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

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

      

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

+9


source


Try the following:



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

      

+1


source







All Articles