Title: how to merge two files in unix
Tags: shell;unix
Question: I want to merge two files in Unix. How can I do this?

eg file1 contains:

```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 contains:

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

output result:

```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
```

I have used this command ```paste -d ':' file1 file2 &gt;merged_file```, but this is what I am getting:

```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
```


Here is the accepted answer: Use ```join``` instead:

```% 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
```

Comment for this answer: @pratik That can be fixed by sorting around the first field before joining. The true question is: are you trying to just merge the files linewise or are you actually trying to join them around the first column?
Comment for this answer: `join` works but suppose if file2 is not in sorted order then it gives error . Can we use sed or awk
Comment for this answer: i also want to merge file on first column basics .


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/27/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<shell><unix>', 'Title': 'how to merge two files in unix', 'ViewCount': 413, 'id': 26584098, 'user_AboutMe': '', 'user_AccountId': 4579583.0, 'user_DisplayName': 'pratik', 'user_Reputation': 47.0, 'user_Views': 18.0, 'user_id': 3717037.0}}