compare and redirect to new file

hi i have two file

a.txt
123,b,c,d,e
111,c,d,e,f,
456,a,k,j,h

b.txt
123
678
987
321
456

i want to compare these two files(match content of b first coloum with a ) and o/p should be like
123,d,e
456,j,h

pls help.....:slight_smile:

Hi,

Try this,

grep -f b.txt a.txt | awk -F, '{print $1","$4","$5}'

Regards,
Chella

thanks for reply but grep -f is not supported in my system

Then it can be done using awk,

awk -F, 'FNR==NR {a[$1];next;}($1 in a) {print $1","$4","$5}' b.txt a.txt

Regards,
Chella

hi its giving error

cat a.txt
123,b,c,d,e
111,c,d,e,f,
456,a,k,j,h

cat b.txt
123
678
987
321
456

awk -F, 'FNR==NR {a[$1];next;}($1 in a) {print $1","$4","$5}' b.txt a.txt
awk: syntax error near line 1
awk: bailing out near line 1

But the awk works fine for me and I got your expected output.

Try this,

awk -F, 'NR==FNR{a[$1]=$1;next}a[$1]' b.txt a.txt

From the above output you need to print only the first,fourth and fifth fields.

Regards,
Chella

Use nawk or /usr/xpg4/bin/awk on Solaris.

thanks its working now.........:slight_smile: