Transpose column to row - awk

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
bash-3.2$ cat txt
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
 
bash-3.2$ awk -F, 'NR==1{ {for(i=1;i<=NF;i++){ x=$i }} } END {for(c in x){ print x[c]"="$c }}' txt
cu_003_mass=9385512
cu_003_volume=3028000
cu_003=1.98
cu_002_mass=9375123
cu_002_volume=3026200
cu_002=1.54
cu_001_mass=9374400
cu_001_volume=3024000
cu_001=1.33
source=ja116

If you just want cu_XXX

bash-3.2$ awk -F, 'NR==1{ {for(i=1;i<=NF;i++){ if($i ~ /^cu_[0-9]{3}$/){x=$i} }} } END {for(c in x){ print x[c]"="$c }}' txt
cu_003=1.98
cu_002=1.54
cu_001=1.33
1 Like
a=[]
with open("a.txt") as f:
 for line in f:
  words = line.split(",")
  a.append(words[1::3])
for i in range(len(a)):
 if(i==0): 
  pass
 else:
  for j in range(len(a[0])):
   print(a[0][j],a[j])
1 Like

Thanks for the reply's and extra examples, much appreciated :slight_smile: