-1

That is, turn this:

Column_A   Column_B   Column_C
--------   --------   --------
value-1a   value-1b   value-1c
value-2a   value-2b   value-2c

into this:

Column_A: value-1a
Column_B: value-1b
Column_C: value-1c

Column_A: value-2a
Column_B: value-2b
Column_C: value-2c
0
4

If the above table is in a file named table:

$ awk 'NR==1 { for (i=1;i<=NF;i++) header[i]=$i }
       NR>2  { for (i=1;i<=NF;i++) print header[i] ": " $i
               print ""}' table

Headers are collected in the first line. The second line is skipped. The remaining lines are printed as shown.

It's not very elegant but does the job, I think.

0
2
$ cat tst.awk
BEGIN { OFS="\n"; ORS="\n\n" }
NR == 1 { split($0,tags) }
NR > 2 {
    for (i=1; i<=NF; i++) {
        printf "%s: %s%s", tags[i], $i, (i<NF ? OFS : ORS)
    }
}

$ awk -f tst.awk file
Column_A: value-1a
Column_B: value-1b
Column_C: value-1c

Column_A: value-2a
Column_B: value-2b
Column_C: value-2c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.