linux - 如何比较两个列表的第一个词并打印匹配条目的第二个词?

标签 linux bash shell unix

你好,举个例子,我有以下文件:

file1
aa1
aa2
aa3
aa4
aa5
aa6
aa7
aa8
aa9
aa10

file2
aa1 1.1.1.1
aa2 1.1.1.2
aa4 1.1.1.4
aa6 1.1.1.6
aa11 1.1.1.11
aa8 1.1.1.8

我需要保留 file1 的所有行,但打印匹配行的 IP。像这样的东西:

file3
aa1 1.1.1.1
aa2 1.1.1.2
aa3
aa4 1.1.1.4
aa5
aa6 1.1.1.6
aa7
aa8 1.1.1.8
aa9
aa10

我尝试使用两个 for 循环和 awk 来执行一些脚本,但它没有按预期工作。

最佳答案

使用标准的 join 命令:

$ join -a 1 file1 file2
aa1 1.1.1.1
aa2 1.1.1.2
aa3
aa4 1.1.1.4
aa5
aa6 1.1.1.6
aa7
aa8 1.1.1.8
aa9
aa10

此工具将两个文件连接到一个共享的“键”列中。 -a 1 保留文件 1 中所有未配对的行。

关于linux - 如何比较两个列表的第一个词并打印匹配条目的第二个词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207529/

相关文章:

bash - 外壳 : In-file converting first field of a text file from decimal to hexadecimal

bash - $@ 和 "$@"之间有什么区别吗?

bash - 我如何知道在 bash 中使用 awk 首先出现了哪个定界符?

bash - 如何按版本号对文件名进行排序?

C# 派生类不会覆盖基方法

python - 无法使用可靠的用户模块为用户分配密码

macos - Bash 的 command_not_found_handle() 问题

java - 有没有Java版本管理器?

c - 使用 for 循环创建多个线程时的奇怪行为

linux - 如何让 iwconfig 将信号电平显示为 dbm 而不是分数