Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to merge/join or produce a new merged file . Both files contain common IDs in the first column .

File 1 looks like this

ID
Tb927.4.4670
Tb927.8.3630
Tb09.160.4310
Tb927.8.3650

File 2 contains the ID and also functions for each gene and looks like this

ID              Function 1    Other field
Tb09.211.0140     A               eg
Tb11.03.0080      B               eg
Tb927.8.6200      C               eg
Tb927.7.690       D               eg

However, some IDs are missing in my file 2 as not all genes have known function so the fields in my file 2 are less than in my file 1. The IDs in file 2 are also sorted differently from my file 1.

I want my merged file to follow the sequence in my file 1 with merged functions in the output . For the missing ID, it can either be empty or something like return '-' .

Is it possible to merge this kind of files using linux command?

share|improve this question

1 Answer 1

using awk

awk 'NR == FNR{a[$1]=$0; next};
     ($1 in a){print a[$1]; next};
      {print $1, "-", "-"}' file2 file1

Or slightly terser

awk 'NR == FNR{a[$1]=$0; next};
  {print $1 in a?a[$1]:$1FS"-"FS"-"}' file2 file1
share|improve this answer
    
somehow that didn't work for me. i've got something like this : ID - - Tb927.4.4670 - - Tb927.8.3630 - - Tb09.160.4310 - - Tb927.8.3650 - - Tb927.8.3560 - - Tb927.8.3620 - - Tb927.6.4970 - - Tb927.1.2110 - - Tb927.2.3030 - - Tb09.160.2280 - - Tb927.7.2180 - - Tb11.01.1300 –  user3072782 Apr 4 at 21:27
    
@user3072782, what character are your fields delimited by? –  1_CR Apr 4 at 21:42
    
tab-delimited . –  user3072782 Apr 4 at 21:58
    
@user3072782, works like a charm with tab-delimited files at this end –  1_CR Apr 4 at 22:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.