This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to add column name to each cell using bash?

I have a table (as .csv) with rows (40) as gene names and columns (157) as the species they belong to (link to an example here: https://imgur.com/a/lCpc2uy )

I want to add the name of the column to each cell of that particular column. How do I go about doing it?

Example of the dataset (of how I want it to be):

https://imgur.com/a/xfcAKEi

bash linux awk

nitinra: Please copy and post the test data here (use 10101 button to format it properly) or paste the images in (we discourage that but see reason below).

The image you posted to an external image provider will be lost in time and this question will lose its context.

2 answers

Hi! I think the following awk command should do the trick:

awk 'BEGIN{FS=","}NR==1 {for (i=1; i<=NF; i++) {colnames[i] = $i;}print}NR>1{for(i=1; i<=NF; i++){$i=colnames[i]"_"$i;printf "%s,", $i};print '\n'}' input.csv

If your input file is tab separated change FS="," to FS="\t".

Try csvtk replace

$ cat input.tsv 
A       B       C
1       2       3
4       5       6

csvtk headers -t input.tsv \
    | while read col; do \
        csvtk replace -t -f $col -p "(.+)" -r "${col}_\$1" input.tsv \
            | csvtk cut -t -f $col > col_$col.tsv ; \
    done

$ paste col_*.tsv
A       B       C
A_1     B_2     C_3
A_4     B_5     C_6

Log in to answer this question.