Add column header and row header

Hi,

I have an input like this

1 2 3 4
2 3 4 5
4 5 6 7

I would like to count the no. of columns and print a header with a prefix "Col".

I would also like to count the no. of rows and print as first column with each line number with a prefix "Row"

So, my output would be

        Col1 Col2 Col3 Col4
Row1     1     2     3     4
Row2     2     3     4     5
Row3     4     5     6     7

A crude first suggestion, that you could refine as you see fit:

awk 'NR==1{for(i=1; i<=NF; i++) h=h OFS "Col" i; print h}{$1="Row" NR OFS $1}1' OFS='\t' file
1 Like
awk ' {
        if(!c)
        {
                for (i = 1; i <= NF; i++)
                {
                        printf "\tCol%d", i
                }
        }

        c = 1

        printf "\nRow%d\t", NR

        for (i = 1; i <= NF; i++)
        {
                printf "%d\t", $i
        }

} END {
        printf "\n"
} ' file