Hi there,
I have a small csv file example below:
source,cu_001,cu_001_volume,cu_001_mass,cu_002,cu_002_volume,cu_002_mass,cu_003,cu_003_volume,cu_003_mass
ja116,1.33,3024000,9374400,1.54,3026200,9375123,1.98,3028000,9385512
I want to transpose columns to rows starting at the second column and incrementing every third column. Desired output would be:
cu_001,1.33
cu_002,1.54
cu_003,1.98
My attempt is as follows:
gawk -F"," '{for (i=2; i<=NF; i+=3) col = col "," $i} END {for (i=2; i<=NF; i+=3) {print col}}' OFS=, input_file.csv | gawk -F"," 'FNR != 1 {print $2","$3}' OFS=, > output_file.csv
However, the output I get is:
cu_002,1.54
cu_003,1.98
It's missing:
cu_001,1.33
Any help would be appreciated:)
---------- Post updated at 07:29 AM ---------- Previous update was at 06:41 AM ----------
I figured it out. I just needed to get rid of FNR != 1. Edited code is:
gawk -F"," '{for (i=2; i<=NF; i+=3) col = col "," $i} END {for (i=2; i<=NF; i+=3) {print col}}' OFS=, input_file.csv | gawk -F"," '{print $2","$3}' OFS=, > output_file.csv