[英]How to compare the columns of file1 to the columns of file2, select matching values, and output to new file using grep or unix commands
I have two files, file1 and file2, where the target_id compose the first column in both.我有两个文件,file1 和 file2,其中 target_id 构成两个文件的第一列。
I want to compare file1 to file2, and only keep the rows of file1 which match the target_id in file2.我想比较 file1和file2,只保留 file1 中与 file2 中的 target_id 匹配的行。
file2:文件2:
target_id
ENSMUST00000128641.2
ENSMUST00000185334.7
ENSMUST00000170213.2
ENSMUST00000232944.2
Any help would be appreciated.任何帮助,将不胜感激。
% grep -x -f file1 file2
resulted in no output in my terminal % grep -x -f file1 file2
在我的终端中没有 output
Sample data that actually shows overlaps between the files.实际显示文件之间重叠的样本数据。
file1.csv
: file1.csv
:
target_id,KO_1D_7dpi,KO_2D_7dpi ENSMUST00000178537.2,0,0 ENSMUST00000178862.2,0,0 ENSMUST00000196221.2,0,0 ENSMUST00000179664.2,0,0 ENSMUST00000177564.2,0,0
file2.csv
target_id ENSMUST00000178537.2 ENSMUST00000196221.2 ENSMUST00000177564.2
Your grep
command, but swapped:您的
grep
命令,但已交换:
$ grep -F -f file2.csv file1.csv
target_id,KO_1D_7dpi,KO_2D_7dpi
ENSMUST00000178537.2,0,0
ENSMUST00000196221.2,0,0
ENSMUST00000177564.2,0,0
Edit : we can add the -F
argument since it is a fixed-string search.编辑:我们可以添加
-F
参数,因为它是固定字符串搜索。 Plus it adds protection against the .
此外,它还增加了针对
.
matching something else as a regex.匹配其他东西作为正则表达式。 Thanks to @Sundeep for the recommendation.
感谢@Sundeep 的推荐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.