Comparing files in bash script
Hello,
I want to compare two files and display values unique to file1. I tried using comm but it did not give me useful outputs. comm -2 -3 file1 file2 does not work Similar threads provide matched content. What I am looking for is unique content |
How about diff?
|
Diff performs a line by line check whereas situation can be as following:
file1 1 2 3 4 5 file2 4 3 2 5 I need the output to be 1 because it is unique to file1. Whereas diff performs a line by line search and gives the following result diff file1 file2 1,4d0 < 1 < 2 < 3 < 4 5a2,4 > 4 > 3 > 2 |
Hi,
Both comm and diff only work on sorted files. If you sort file 2 then comm -23 file1 file2 will give you the answer you are looking for. Hope this clears things up a bit. |
How about using the option
Code:
comm file-1 file-2 outputs three columns: Code:
comm -23 file1 file2 Kind regards |
@repo: The OP's command given in the initial post works _if_ both files are sorted. I don't see how your comment solves the unsorted input file(s), I do think s/he looked at the man page to come up with: comm -2 -3 file1 file2
|
Quote:
|
Thanks druuna ... this was the missing link :) I was using it on unsorted files
|
You're welcome :)
|
Hi.
I often find it irritating to need to sort files for comm, join, etc. One can write scripts to that, of course. However, occasionally, there exists a utility that is designed to handle such situations. In this case command combine can do this. For your data on files data1 and data2: Code:
combine data1 not data2 Code:
1 http://kitenet.net/~joey/code/moreutils/ A quick glance with zypper on openSUSE 11.3 "Teal" did not find it, but yum on Fedora release 13 (Goddard) did find it. Don't confuse it with perl module List::MoreUtils. Best wishes ... cheers, makyo The environment was: Code:
Environment: LC_ALL = C, LANG = C |
All times are GMT -5. The time now is 10:08 PM. |