Add columns to a file based on matched column and maintain the order of lines using awk
0
0
Entering edit mode
22 months ago
AP ▴ 80

How can I match the column to a file to next file and append the columns if present using awk.

File 1:

    T36
    T200
    T77
    T99
    T100
    T101
    T110

File2:

T36 aa  123
T36 aa  456
T200 cc 789
T99 aa  1011
T77 bb  1213
T77 bb  1415
T100 xx 1617

Desired output:

T36 aa  123
T36 aa  456
T200    cc  789
T77 bb  1213
T77 bb  1415
T99 aa  1011
T100    xx  1617
T101
T110

In my output file I want to get the order of file 1 and also print the lines that are not present in file 2.

So far I have done this but it is printing only the matched columns not all.

awk 'NR == FNR { x[$1]=$1; next} { print x[$1], $0 }' file1 file2

Please Help!!

shell awk bash • 1.1k views
ADD COMMENT
1
Entering edit mode

how is it related to bioinformatics ??

by the way, you just want join. https://linux.die.net/man/1/join

ADD REPLY
1
Entering edit mode

... and the very same question was asked yesterday... Compare two columns from two files and print the content of both the files

ADD REPLY
0
Entering edit mode

Pierre, looks like join needs a sorted file. I want to maintain the order same as the first file without sorting. And thanks for the information.

ADD REPLY
1
Entering edit mode

as I said, the very same question was asked yesterday : Compare two columns from two files and print the content of both the files

I want to compare the 16th column of file1 with 1st column of file2, while maintaining the order of file1 and print the content of both the files.

ADD REPLY

Login before adding your answer.

Traffic: 2577 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6