Find the similarities between two files

后端 未结 2 1174
独厮守ぢ
独厮守ぢ 2020-12-07 00:02

I have two file like:

1
2
3
4
5
6
7
8
9
10

and a file with two columns

1 0.11
2 0.12748
5 0.45
12 0.48
7 0.48
8 0.7
13 0.78         


        
相关标签:
2条回答
  • 2020-12-07 00:34

    You can use grep -f patt_file file to get the patterns from a file. That is, search in file patterns included in patt_file.

    See the output with your given input:

    $ grep -w -f file1 file2
    1 0.11
    2 0.12748
    5 0.45
    7 0.48
    8 0.7
    

    From man grep:

    -f FILE, --file=FILE

    Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.

    0 讨论(0)
  • 2020-12-07 00:35

    With awk:

    awk 'NR==FNR{a[$1]++;next}$1 in a' test.1 test.2
    

    Note in an array the lines appearing in the first file, then print lines in the second that were in the first. Lines will appear in the output in the order they appear in the second file.

    Or join:

    join <(sort test.1) <(sort test.2)
    

    The files need to be sorted. Lines will appear in dictionary order.

    0 讨论(0)
提交回复
热议问题