1

I have a file (file-1) that looks like this,

DIP-10097N|refseq:NP_416170|uniprotkb:P30015
DIP-10117N|refseq:NP_414973|uniprotkb:P08177
DIP-10168N|refseq:NP_418766|uniprotkb:P15005
DIP-10199N|refseq:NP_415632|uniprotkb:P30958
DIP-10358N|refseq:NP_418659|uniprotkb:P28903
DIP-10440N|refseq:NP_289596|uniprotkb:P20082
DIP-10441N|refseq:NP_417502|uniprotkb:P20083
DIP-10441N|refseq:NP_417502|uniprotkb:P20083
DIP-10467N|refseq:NP_415423|uniprotkb:P09373
DIP-10469N|refseq:NP_418386|uniprotkb:P32674
DIP-10562N|refseq:NP_418370|uniprotkb:P17888
DIP-10582N|refseq:NP_414864|uniprotkb:P77743
DIP-10592N|refseq:NP_415819|uniprotkb:P37344

and another (file-2) which looks like this,

DIP-10331N|refseq:NP_311078|uniprotkb:P12638     DIP-10117N|refseq:NP_414973|uniprotkb:P08177
DIP-10331N|refseq:NP_311078|uniprotkb:P12638    DIP-10840N|refseq:NP_414640|uniprotkb:P10408
DIP-1025N|refseq:NP_414574|uniprotkb:P00968     DIP-10097N|refseq:NP_416170|uniprotkb:P30015
DIP-10467N|refseq:NP_415423|uniprotkb:P09373    DIP-10097N|refseq:NP_416170|uniprotkb:P30015
DIP-10117N|refseq:NP_414973|uniprotkb:P08177    DIP-10117N|refseq:NP_414973|uniprotkb:P08177
DIP-10117N|refseq:NP_414973|uniprotkb:P08177    DIP-10750N|refseq:NP_289799|uniprotkb:P02410
DIP-10117N|refseq:NP_414973|uniprotkb:P08177    DIP-10757N|refseq:NP_288150|uniprotkb:P02421

In output I want to print the contents of file-1 plus the value in either column of file-2 that has the same value as that of file-1 in the other column. Like this,

DIP-10097N|refseq:NP_416170|uniprotkb:P30015 DIP-1025N|refseq:NP_414574|uniprotkb:P00968
DIP-10097N|refseq:NP_416170|uniprotkb:P30015 DIP-10467N|refseq:NP_415423|uniprotkb:P09373
DIP-10117N|refseq:NP_414973|uniprotkb:P08177 DIP-10117N|refseq:NP_414973|uniprotkb:P08177
DIP-10117N|refseq:NP_414973|uniprotkb:P08177 DIP-10750N|refseq:NP_289799|uniprotkb:P02410
DIP-10117N|refseq:NP_414973|uniprotkb:P08177    DIP-10757N|refseq:NP_288150|uniprotkb:P02421
DIP-10117N|refseq:NP_414973|uniprotkb:P08177 DIP-10331N|refseq:NP_311078|uniprotkb:P12638
DIP-10467N|refseq:NP_415423|uniprotkb:P09373 DIP-10097N|refseq:NP_416170|uniprotkb:P30015

Is there a way I can do it using awk or grep. Any help would be highly appreciated.

1 Answer 1

3

This is wide known operation in awk — collect array from key-file then use the array to operate with second file values

awk '
    FNR==NR{
            A[$2] = A[$2] " " $1
            next
    }
    $1 in A{
            for(i=1;i<=split(A[$1], B);i++)
                print $1 B[i]
    }
    ' file2 file1

Or a little bit shorter:

awk '
    FNR==NR{
            A[$2] = A[$2] $2 " " $1 "\n"
            next
    }
    $1 in A{
            printf "%s", A[$1]
    }
    ' file2 file1

Other variant

grep -f <(cat -E file1) file2 |
sed 's/\(\S*\)\s*\(\S*\)/\2\t\1/' |
sort

At last easiest (as for me):

join -2 2 file1 <(sort -k2 file2)
2
  • Thanks. All of these work. Can you be so kind to explain the last code please. Commented Nov 2, 2015 at 10:47
  • To join files its should be sorted by join field. As we can see file1 is sorted already so we'l sort file2 by second field(-k2 option) only. Then we join two files by first field file1 (which have just 1) as default and file2 by second field(option -2 2). So output will be: field 1 from file1(which equal field 2 from file2) and residue — corespondent field 1 from file2 Commented Nov 2, 2015 at 17:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.