I have a csv file called scenario1.csv in which the second column has column names like "0-4 years high risk", "65+ years first responder" etc. There are 20 such values. The 21st row, 2nd column has the same entry as in the 1st row of the 2nd column. I would like to rename these values to p1-p20 respectively. So the 21st row would have the p1 tag with it. Everything without quotes.
I have a 150 such files, named scenario1.csv, scenario2.csv … How do I do this? Below is an example for a shorter file:
t, group, 1, 3, 5
0, 0-4 years low risk, 0, 0, 0
0, 0-4 years high risk, 0, 0, 1
....., ....
0, 0-4 years low risk, 0, 0, 0
Expected output for each file:
t, group, 1, 3, 5
0, p1, 0, 0, 0
0, p2, 0, 0, 0
....., ....
0, p1, 0, 0, 0
Here's the dictionary I need:
0-4 years first responder p1
0-4 years high risk p2
.......
65+ years low risk p19
65+ years pregnant women p20
Best Answer
Since you don't have GNU AWK nor
sponge
installed:-F ','
: sets the input field separator to,
;-v OFS=','
: sets the output field separator to,
;NR!=1{$2="p"(NR-2)%20+1}1
: if the current record number is greater than1
, sets the second field to a string made of ap
character followed by the result of the expression(NR-2)%20+1
and prints the record;To repeat this for all the files matching the globbing pattern
treatables-???.csv
in the current working directory you may use a Bashfor
loop: