How to add column name to each cell using bash?
2
0
Entering edit mode
2.1 years ago
nitinra ▴ 50

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 • 1.6k views
ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
4
Entering edit mode
2.1 years ago
iraun 6.2k

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".

ADD COMMENT
2
Entering edit mode
2.1 years ago

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
ADD COMMENT

Login before adding your answer.

Traffic: 1802 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6