如何在unix中合并两个文件

时间:2014-10-27 09:04:16

标签: shell unix

我想在Unix中合并两个文件。我怎么能这样做?

例如file1包含:

host1:90:/users:user1 
host2:90:/users:user1 
host3:90:/users:user1 
host4:90:/users:user1
host5:90:/users:user1 
host6:90:/users:user1 
host7:90:/users:user1

file2包含:

host1:owner_name
host2:owner_name
host3:owner_name
host4:owner_name
host5:owner_name
host6:owner_name
host7:owner_name

输出结果:

host1:90:/users:user1:owner_name
host2:90:/users:user1:owner_name
host3:90:/users:user1:owner_name
host4:90:/users:user1:owner_name
host5:90:/users:user1:owner_name
host6:90:/users:user1:owner_name
host7:90:/users:user1:owner_name

我使用了这个命令paste -d ':' file1 file2 >merged_file,但这就是我得到的:

host1:90:/users:user1:host1:owner_name
host2:90:/users:user1:host2:owner_name
host3:90:/users:user1:host3:owner_name
host4:90:/users:user1:host4:owner_name
host5:90:/users:user1:host5:owner_name
host6:90:/users:user1:host6:owner_name
host7:90:/users:user1:host7:owner_name

1 个答案:

答案 0 :(得分:2)

改为使用join

% join -t':' file1 file2
host1:90:/users:user1:owner_name
host2:90:/users:user1:owner_name
host3:90:/users:user1:owner_name
host4:90:/users:user1:owner_name
host5:90:/users:user1:owner_name
host6:90:/users:user1:owner_name
host7:90:/users:user1:owner_name