Skip to content
Advertisement

partial merging 2 text file in linux

I have 2 text file which I want to merge them in a new file.

my first file is look like

DjScaffold1002  blastx  protein_match   50305   51044   125 -   .   ID=DjScaffold1002:hit:2832:3.10.0.0;Name=sp|Q9UEW3|MARCO_HUMAN
DjScaffold1002  blastx  protein_match   50305   50892   208 -   .   ID=DjScaffold1002:hit:2833:3.10.0.0;Name=sp|Q60754|MARCO_MOUSE
DjScaffold1002  blastx  protein_match   44584   45176   145 -   .   ID=DjScaffold1002:hit:2834:3.10.0.0;Name=sp|Q7SIB2|CO4A1_BOVIN

and second file =

Q9UEW3  MARCO
Q60754  Marco
Q7SIB2  COL4A1

and my expected output like, I mean mapping part of 9th column string from first line with 1st column of second file, should be like :

DjScaffold1002  blastx  protein_match   50305   51044   125 -   .   ID=DjScaffold1002:hit:2832:3.10.0.0;Name=sp|Q9UEW3|MARCO_HUMAN;Symbol=MARCO
DjScaffold1002  blastx  protein_match   50305   50892   208 -   .   ID=DjScaffold1002:hit:2833:3.10.0.0;Name=sp|Q60754|MARCO_MOUSE;Symbol=Marco
DjScaffold1002  blastx  protein_match   44584   45176   145 -   .   ID=DjScaffold1002:hit:2834:3.10.0.0;Name=sp|Q7SIB2|CO4A1_BOVIN;Symbol=COL4A1

Also, I wanted to keep line from first file which were not mapped with second file.

How can perform that with awk?

Advertisement

Answer

You can use awk to merge them:

awk 'FNR == NR{a[$1]=$2; next} $2 in a{$0 = $0 ";symbol=" a[$2]} 1' f2 FS='[|]' f1

L1  code=c|id1|;name=name1;symbol=symbol1
L2  code=c|id2|;name=name1;symbol=symbol2
L0  code=c|id0|;name=name0;

For your edited samples:

awk 'FNR == NR{a[$1]=$2; next} $2 in a{$0 = $0 ";symbol=" a[$2]} 1' f2 FS='[|]' f1

DjScaffold1002  blastx  protein_match   50305   51044   125 -   .   ID=DjScaffold1002:hit:2832:3.10.0.0;Name=sp|Q9UEW3|MARCO_HUMAN;symbol=MARCO
DjScaffold1002  blastx  protein_match   50305   50892   208 -   .   ID=DjScaffold1002:hit:2833:3.10.0.0;Name=sp|Q60754|MARCO_MOUSE;symbol=Marco
DjScaffold1002  blastx  protein_match   44584   45176   145 -   .   ID=DjScaffold1002:hit:2834:3.10.0.0;Name=sp|Q7SIB2|CO4A1_BOVIN;symbol=COL4A1
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement