LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Comparing files in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/comparing-files-in-bash-script-829890/)

brainlesseinstein 09-02-2010 02:49 AM

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

grail 09-02-2010 02:55 AM

How about diff?

brainlesseinstein 09-02-2010 03:02 AM

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

druuna 09-02-2010 03:10 AM

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.

repo 09-02-2010 03:19 AM

How about using the option
Code:

comm file-1 file-2 outputs three columns:
column 1 = lines unique to file-1
column 2 = lines unique to file-2
column 3 = lines common to both.

-1 suppresses column 1
-2 suppresses column 2
-3 suppresses column 3
-12 suppresses both columns 1 and 2, etc.


Code:

comm -23 file1 file2
See man com
Kind regards

druuna 09-02-2010 03:26 AM

@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

repo 09-02-2010 03:29 AM

Quote:

Originally Posted by druuna (Post 4085795)
@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

Correct

brainlesseinstein 09-02-2010 03:45 AM

Thanks druuna ... this was the missing link :) I was using it on unsorted files

druuna 09-02-2010 03:45 AM

You're welcome :)

makyo 09-02-2010 12:41 PM

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
produces:
Code:

1
The command combine was found in package moreutils in the Debian 5 ("lenny") repository, and the latter can also be found at
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
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0
GNU bash 3.2.39
combine - ( /usr/bin/combine Jun 28 2008 )



All times are GMT -5. The time now is 10:08 PM.