0

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?

2 Answers 2

2

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
4
  • 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 Apr 4, 2014 at 21:27
  • @user3072782, what character are your fields delimited by?
    – iruvar
    Apr 4, 2014 at 21:42
  • tab-delimited . Apr 4, 2014 at 21:58
  • @user3072782, works like a charm with tab-delimited files at this end
    – iruvar
    Apr 4, 2014 at 22:24
0

Just use sort(1) on your lot of files, the program is smart enough to just merge if the files are already sorted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .