简体   繁体   English

如何使用 grep 或 unix 命令将 file1 的列与 file2 的列、select 匹配值以及 output 与新文件进行比较

[英]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.我想比较 file1file2,只保留 file1 中与 file2 中的 target_id 匹配的行

文件1

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.

相关问题 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件? - How to append each column of file1 to a specific field of file2 and make a new output file? 如何从从 file1 查询到 file2 的匹配结果中编写具有不同列的 file3? - How can I write a file3 with different columns from a matched result queried from file1 to file2? 可能是grep,但仍然无法获得如何读取file1中的行并将其粘贴为file2中的列的方法 - Probably grep, but still do not get how to read row in file1 and paste it as a column in file2 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 如果在file2中找不到file1的输出行 - Output line from file1 if not found in file2 如何使用Unix命令为特定列查找具有相同值的文件中的行? - How to find rows in a file with same values for specific columns using unix commands? 如何根据与file2的列匹配删除file1中的行 - How to delete lines in file1 based on column match with file2 如何使用awk删除Ubuntu的file2中存在列1值的file1行? - How to use awk to delete lines of file1 whose column 1 values exist in file2 in Ubuntu? 比较2个Unix文件和输出匹配行到新文件? - Compare 2 Unix Files and Output Matching Lines to a New File?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM