BEGIN{RS="";FS=","; ORS="\n\n"} FNR==NR{for (i=1;i<=NF;i++) {ind[i]=$i+0}} #make clever use of RS="" to read entire file FNR!=NR{rec[++j]=$0} END{for (i=1;i<=length(ind);i++) {print rec[ind[i]]}}