如何保持以数字列表开头的行(Linux)?

发布于 2025-01-13 20:13:33 字数 430 浏览 1 评论 0原文

我有一个如下所示的文件:

0   123   word
1   324   word
2   234   word
3   53    word
4   666   word
5   23    word
6   4     word
...

我一直试图仅保留以某些数字开头的行。我的文件中的数字如下所示:

2
4
5
...

因此最终文件应如下所示:

2   234   word
4   666   word
5   23    word
...

我想我可以使用 grep (grep "2" input > output) 获取以特定字符开头的行,但是通过以下方式进行操作一个不可行,我不知道如何对文件中的数字列表执行此操作。有人可以帮我吗?

I have a file that looks like this:

0   123   word
1   324   word
2   234   word
3   53    word
4   666   word
5   23    word
6   4     word
...

I have been trying to keep only the lines starting with some numbers. I have the numbers in a file that looks like this:

2
4
5
...

So the final file should look like this:

2   234   word
4   666   word
5   23    word
...

I think I can get lines starting with a specific character using grep (grep "2" input > output), but doing it one by one is not viable and I do not know how to do it for a list of numbers in a file. Could someone help me with that?

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

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

发布评论

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

评论(1

抽个烟儿 2025-01-20 20:13:33

使用 GNU awk:

awk 'NR == FNR {a[$1]; next} $1 in a {print}' file1 file2

其中 file1 包含要保留的数字,每行一个,file2 是要过滤的文件。

With GNU awk:

awk 'NR == FNR {a[$1]; next} $1 in a {print}' file1 file2

where file1 contains the numbers to keep, one per line, and file2 is the file to filter.

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