user3037937 user3037937 - 2 years ago 76
Bash Question

Extract column 'x' from multiple files, and transpose file name with 'x'

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


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
file which looks like:

file1 geneX geneY geneZ
file2 geneA geneY

Total row numbers may vary between files. I tried using
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")} '
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download