I need to compare 2 files. I need to see if 1 file has records that are not in a second file. I did some searching and found the 'comm' command. According to the man pages
comm -23 test1.txt test2.txt
Will tell me what is in file 1 and not in file 2. So I did a simple test
test1.txt has the following data
2
3
4
1
test2.txt
1
2
3
> comm -23 test1.txt test2.txt
4
1
It looks like comm doesn't work if the fields are ordered differently. How do I do this?
joeyg
May 2, 2012, 11:32am
2
Thus, you should sort each file prior to doing a COMM command.
drl
May 2, 2012, 4:35pm
3
Hi.
There are many times when I don't want to sort the comm input files. So then I use combine, part of moreutils to be found at: moreutils
Here's a comparison:
#!/usr/bin/env bash
# @(#) s1 Demonstrate work-alike for comm, not requiring sort.
# http://kitenet.net/~joey/code/moreutils/
# Display context; "version" is local utility.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
pe
version 2>/dev/null && version =o perl comm ./combine
pe " Utility combine is $( wc -l < combine ) lines of perl."
pl " Input data files data[12]:"
head data[12]
pl " Results with comm, unsorted:"
comm -23 data[12]
pl " Results with comm, sorted:"
comm -23 <( sort data1 ) <( sort data2 )
pl " Results with combine, unsorted:"
./combine data1 not data2
exit 0
producing:
$ ./s1
OS, ker|rel, machine: HP-UX, B.11.11, 9000/712
Distribution : GenericSysName [HP Release B.11.11] (see /etc/issue)
perl 5.8.0
comm - ( /usr/bin/comm Nov 14 2000 )
./combine - (local: ./combine May 2 21:15 )
Utility combine is 165 lines of perl.
-----
Input data files data[12]:
==> data1 <==
2
3
4
1
==> data2 <==
1
2
3
-----
Results with comm, unsorted:
4
1
-----
Results with comm, sorted:
4
-----
Results with combine, unsorted:
4
I don't know if all the moreutils work on HP-UX, but, as you can see, combine does.
Best wishes ... cheers, drl
( edit 1: replace xor with not [same result in this instance] )
Not necessary to install a new package when the supplied utilties suffice.
Post #2 has the solution. Sort both files before running comm .