Patches
Patches

Reputation: 49

How do I merge two files so that equal lines do not repeat?

Let's say I have two files like this:

File1: A B C

File2: D C B

The result file should be like: A B C D (order doesn't matter).

I could google that up if I knew exactly the name of this mechanic (it should probably have one, to me it looks like an OR). Using linux command merge/cat file1 file2 > file3 outputs every single line like this A B C D C B but man pages of those two commands do not mention anything helpful for the purpose. I'd like to have an elegant solution like [command] [parameter] file1 file2 > file3 since I can write a bash script to do that but it seems pretty overkill.

Upvotes: 0

Views: 240

Answers (2)

Walter A
Walter A

Reputation: 20032

When you do not need the output sorted, you can skip that step:

awk '{a[$0]} END {for (key in a) print key;}' file[12]

Upvotes: 0

l0b0
l0b0

Reputation: 58988

This will concatenate, then sort, then remove duplicate lines:

LC_ALL=C sort -u input1.txt input2.txt > output.txt

Upvotes: 3

Related Questions