从另一个文件中的一个文件中查找模式

发布于 2025-01-24 04:33:55 字数 1013 浏览 2 评论 0原文

我正在尝试从另一个文件中的一个文件中找到模式。

文件中的模式看起来像这样:

ENSG00000203875.13
ENSG00000262691.1
ENSG00000254911.3

文件两个包含:

ENSG00000203875.13 aa aaa bbb cc
ENSG00000227782.2
ENSG00000229582.3
ENSG00000241769.7
ENSG00000245904.4
ENSG00000254823.2
ENSG00000254911.3 cc ccc ccc
ENSG00000260213.6
ENSG00000260997.1
ENSG00000261799.1
ENSG00000262691.1 bbb bbb bbb
ENSG00000267249.1
ENSG00000270012.1
ENSG00000270091.1
ENSG00000270361.1
ENSG00000271533.1
ENSG00000271833.1
ENSG00000271870.1
ENSG00000272379.1
ENSG00000272631.1
ENSG00000273066.5
ENSG00000273855.1
ENSG00000278966.2
ENSG00000279332.1
ENSG00000279407.1
ENSG00000279616.1
ENSG00000279684.1
ENSG00000279835.1
ENSG00000286181.1
ENSG00000286986.1
ENSG00000287817.1

我试图仅

ENSG00000203875.13 aa aaa bbb cc
ENSG00000254911.3 cc ccc ccc
ENSG00000262691.1 bbb bbb bbb

作为输出找到。我很确定grep -f file_one.txt file_two.txt应该做这项工作,但是我只是将file_two的内容作为输出。我不知道我在犯什么错误。有人可以指出吗?

I'm trying to find patterns from one file in another file.

The pattern in file one looks something like this:

ENSG00000203875.13
ENSG00000262691.1
ENSG00000254911.3

File two contains:

ENSG00000203875.13 aa aaa bbb cc
ENSG00000227782.2
ENSG00000229582.3
ENSG00000241769.7
ENSG00000245904.4
ENSG00000254823.2
ENSG00000254911.3 cc ccc ccc
ENSG00000260213.6
ENSG00000260997.1
ENSG00000261799.1
ENSG00000262691.1 bbb bbb bbb
ENSG00000267249.1
ENSG00000270012.1
ENSG00000270091.1
ENSG00000270361.1
ENSG00000271533.1
ENSG00000271833.1
ENSG00000271870.1
ENSG00000272379.1
ENSG00000272631.1
ENSG00000273066.5
ENSG00000273855.1
ENSG00000278966.2
ENSG00000279332.1
ENSG00000279407.1
ENSG00000279616.1
ENSG00000279684.1
ENSG00000279835.1
ENSG00000286181.1
ENSG00000286986.1
ENSG00000287817.1

I'm trying to find only

ENSG00000203875.13 aa aaa bbb cc
ENSG00000254911.3 cc ccc ccc
ENSG00000262691.1 bbb bbb bbb

as output. I'm pretty sure grep -f file_one.txt file_two.txt should do the job, but instead I just get the content of file_two as output. I don't know what mistake I'm making. Can anyone point it out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2025-01-31 04:33:55

我会做类似的事情:

for i in $(cat file_one.txt); do grep -i $i file_two.txt; done
ENSG00000203875.13 aa aaa bbb cc
ENSG00000262691.1 bbb bbb bbb
ENSG00000254911.3 cc ccc ccc

I'd do something like:

for i in $(cat file_one.txt); do grep -i $i file_two.txt; done
ENSG00000203875.13 aa aaa bbb cc
ENSG00000262691.1 bbb bbb bbb
ENSG00000254911.3 cc ccc ccc
音栖息无 2025-01-31 04:33:55

您可以考虑使用awk方法,跟踪 file_one.txt array a的第一列的值,然后检查 file_two.txt 的第一列的值在数组的键中存在:

awk 'NR==FNR {a[$0]; next} $1 in a' file_one.txt file_two.txt

输出

ENSG00000203875.13 aa aaa bbb cc
ENSG00000254911.3 cc ccc ccc
ENSG00000262691.1 bbb bbb bbb

另一个选项使用GREP:

grep -f file_one.txt file_two.txt

You might consider using an awk approach, keeping track of the values of the first column of file_one.txt in array a, and then check of the value of the first column of file_two.txt is present in the keys of the array:

awk 'NR==FNR {a[$0]; next} $1 in a' file_one.txt file_two.txt

Output

ENSG00000203875.13 aa aaa bbb cc
ENSG00000254911.3 cc ccc ccc
ENSG00000262691.1 bbb bbb bbb

Another option using grep:

grep -f file_one.txt file_two.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文