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
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.
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.