1758160 Members
3331 Online
108868 Solutions
New Discussion юеВ

Re: comm

 
SOLVED
Go to solution
Ngoh Chean Siung
Super Advisor

comm

Hi,

I hv 2 files called 1 and 2.

Content of 1
============
tls
norainij
abcd
tyt

Content of 2
============
abcd

I want to comm these 2 files and the output that I want is only

tls
norainij
tyt

The command that I use is comm -3 1 2 but the output is as below

abcd
tls
norainij
abcd
tyt

May I know what is the problem?

regards.
5 REPLIES 5
RAC_1
Honored Contributor

Re: comm

The command should be
comm -23 file1 file2

Read the man page carefully and pay special attention to exmaples section.
There is no substitute to HARDWORK
Arunvijai_4
Honored Contributor

Re: comm

# man comm says,

comm reads file1 and file2, which should be ordered in increasing
collating sequence (see sort(1) and Environment Variables below), and
produces a three-column output:

Column 1: Lines that appear only in file1,
Column 2: Lines that appear only in file2,
Column 3: Lines that appear in both files.

So, you have to sort file1 before doing anything.

Try this,
# sort one >three (Sort one and store it in three)

# comm -23 three two

norainij
tls
tyt

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor
Solution

Re: comm

You can use sort + uniq as,

# sort file1 file2 | uniq -u

hth.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: comm

You may not use comm to do this job. It can find common lines based 3 1 2 settings. You can use grep as well as,

# grep -vf file2 file1
tls
norainij
tyt

hth.
Easy to suggest when don't know about the problem!
Sandman!
Honored Contributor

Re: comm

comm will do the job as long as the files have been sorted before i.e.

# sort f1 | comm -23 - f2

OR

# sort f1 > f1.out; sort f2 > f2.out; comm -23 f1.out f2.out

regards!