File comparisons

Hi all,

I want to compare two files based on column value
Kindly help me

a.txt
123,ABCD
456,DEF
789,SDF
b.txt
123,KJI
456,LMN
321,MJK
678,KOL

Output file should be like
Common on both files

c.txt
123,ABCD,KJI
456,DEF,LMN

Present in one file but not in 2nd file

789,SDF

Present in 2nd file but not in first file

321,MJK
678,KOL

What have you tried?

I tried using join and awk both but didnt work
I have only basic knowledge of awk :frowning:

In what way did it "not work"? Show exactly what you did, and we can help correct your mistakes.

join -t, -o "1.1 1.2 2.2" a b

Thank you.

join requires sorted input, so isn't suitable here.

How about this:

$ awk -F, '{ A[$1]=A[$1] FS $2 ; T[$1]++ } END { for(X in T) if(T[X]>1) { print X A[X] } }' a.txt b.txt
456,DEF,LMN
123,ABCD,KJI

$
1 Like

can you pls revert for other two requirement as well

Requirement? Is this homework?

Try this:

awk -F, '
NR==FNR{a[$1]=$0;next}
$1 in a{print a[$1] FS $2; delete a[$1];next}
{print > "e.txt"}
END {for(i in a){print a > "d.txt"}}' a.txt b.txt > c.txt