shome February 2016

Combine two columns from different files into one table side-by-side using bash script

I have two files,from which I am trying to extract the third column from each file and combine them together.How to combine them into one table side-by-side?

file1="maize.txt"
file2="wheat.txt"

awk -F '{print $3}' file1
awk -F '{print $3}' file2

"wheat.txt"
chr -   b73_chr10   B73-Chr10   0   150189435       
chr -   w22_chr7    W22-Chr7    0   176764762       
chr -   w22_chr8    W22-Chr8    0   175793759       

"maize.txt"
chr -   w22_chr9    W22-Chr9    0   156750706       
chr -   w22_chr10   W22-Chr10   0   150189435       
chr -   w22_chr6    W22-Chr6    0   169174353   

Output : 

b73_chr10   w22_chr9
w22_chr7    w22_chr10
w22_chr8    w22_chr6

Answers


anubhava February 2016

Using awk you can do:

awk -v OFS='\t' 'FNR==NR{a[FNR]=$3; next} {print a[FNR], $3}' wheat.txt maize.txt
b73_chr10   w22_chr9
w22_chr7    w22_chr10
w22_chr8    w22_chr6


karakfa February 2016

The tool you're looking for is paste. You can write a function to extract the 3rd field as you defined above.

$ f3() { awk '{print $3}' "$1"; }; paste <(f3 wheat.txt) <(f3 maize.txt)

b73_chr10       w22_chr9
w22_chr7        w22_chr10
w22_chr8        w22_chr6

Post Status

Asked in February 2016
Viewed 2,444 times
Voted 11
Answered 2 times

Search




Leave an answer