user3037937 user3037937 - 1 year ago 53
Bash Question

extract column x from multiple file and transpose it with colname as filename

I am trying to extract column "m" from multiple txt files (file1.txt, file2.txt,,,etc) and transpose each column to a row in new file.

below is the original file

contig_1 contig_1 geneX ctg1_886;ctg1_887;ctg1_888
contig_2 contig_2 geneY ctg1_886;ctg1_887;ctg1_888
contig_3 contig_3 genesZ ctg1_886;ctg1_887;ctg1_888

I would like to have a summay.txt file which looks like.

file1 geneX geneY geneZ
file2 geneA geneY

row numbers may vary between files. I tried using awk without success.

Answer Source

Following glenn jackmans advise from the comments, an GNU AWK solution would look like this:

awk 'BEGIN {ORS=" "} BEGINFILE{print FILENAME} {print $3} ENDFILE{ printf("\n")}'  file*.txt

And an awk solution could look like this (sorry only gnu awk for testing):

awk 'BEGIN {ORS=" "} FNR==1 {printf("\n%s", FILENAME)} {print $3} END{printf("\n")} '