简体   繁体   中英

compare two columns of different files and print if it matches linux

I have 2 files

file 1:

contains (114 lines data)

head file1.txt   
    AC002310.2
    AC007298.2
    AL132780.1
    TULP1
    LINC02626
    LINC02211
    AC239809.3
    GTF2F2
    TCF3
    SOX4

file 2: contains 20236 lines, 2 columns, tab separated

head file2.txt
AF130248.1      lncRNA
AC023296.1      lncRNA
AL137139.3      lncRNA
AC114778.2      lncRNA
AL162231.5      lncRNA
Z97205.3        lncRNA
AC010184.1      lncRNA
AL357874.3      lncRNA
AL645933.5      lncRNA
AC116317.2      lncRNA

So I want to compare the 2 files and keep from file 2 only the data that matching with file 1 based on the first column BUT I also want the info from the 2nd column of file 2

I tried the following but it doesn't work

awk -F '\t' 'NR==FNR{c[$1]++;next};c[$1] > 0' file2.txt file1.txt

I will appreciate any help

Your example doesn't provide any matching fields in both files, but this should do it:

awk 'NR==FNR{c[$1]; next} $1 in c' file1 file2

This prints all lines of file2 where the first field is present in file1

Sounds like grep will work:

grep -Ff file1.txt file2.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM