mardi 3 décembre 2019

How can I add one of two random numbers to each row of a csv proportionately?

I have a csv that contains 100 rows by three columns of random numbers:

100, 20, 30
746, 82, 928
387, 12, 287.3
12, 47, 2938
125, 198, 263
...
12, 2736, 14

In bash, I need to add another column that will be either a 0 or a 1. However, (and here is the hard part), I need to have 20% of the rows with 0s, and 80% with 1s. Result:

100, 20, 30, 0
746, 82, 928, 1
387, 12, 287.3, 1
12, 47, 2938, 1
125, 198, 263, 0
...
12, 2736, 14, 1

What I have tried:

sed '1~3s/$/0/' mycsv.csv

but i thought I could replace the 1~3 with 'random number' but that doesn't work. Maybe a loop would? Maybe sed or awk?




Aucun commentaire:

Enregistrer un commentaire