Shell Programming and Scripting

Hi,

Iam having the files as follows:

file1
aa
aa
aa
aa
ab
ac
ad
ae

file2
aa
aa
ab

Outputfile:
aa
aa

Actually i want to track the file1 and file2 for common records.

output file should get the records that are in file1 and not in file2. Here 4 aa's are there in file1. and 2 aa's are in file2. i jus want to get " 2 aa's in file1 and 4 aa's in file2. so we need to track this aa." like this message should come in the o/p file.

Can anybody help??

It should be " 2 aa's in file2 and 4 aa's in file1. so we need to track this aa.". Isn't it ?

And if you need such a message in the o/p file, awk will help you.

i don't want any messages in o/p file. i just want to tract those records that occur in file1 and file2 more than once.

pls give the help

cat file1 | uniq -c | awk '{ if ($1>1) print $2}'

Try:

awk 'NR==FNR{a[$0]++;next}
            ( a[$0] >= 2 ){b[$0]++}
END {
for ( i in  b )
    if ( b >= 2)
       print i
}' file1 file2

This awk command is not showing any output.

awk 'NR==FNR{a[$0]++;next}
( a[$0] >= 2 ){b[$0]++}
END {
for ( i in b )
if ( b [i]>= 2)
print i
}' file1 file2

What plataform ?
Use gawk , or /usr/xpg4/bin/awk in Solaris.

awk command is working and this above command is not producing any result.

awk 'NR==FNR{a[$0]++;next}
( a[$0] >= 2 ){b[$0]++}
END {
for ( i in b )
if ( b [i]>= 2)
print i
}' file1 file2

what we can do to get the output in out.dat file.

Just one line : :b:

paste f1 f2 |awk '{if ($1 == $2) print $1}'

Just one line : :b:

paste f1 f2 |awk '{if ($1 == $2) print $1}'

See the difference:

> cat f1
aa
aa
aa
aa
ab
ac
ac
ad
ae
> cat f2
aa
ab
ac
ac
> paste f1 f2 |awk '{if ($1 == $2) print $1}'
aa

Or:

>awk 'NR==FNR{a[$0]++;next}
            ( a[$0] >= 2 ){b[$0]++}
END {
for ( i in  b )
    if ( b >= 2)
       print i
}' f1 f2
ac
awk 'NR==FNR{a[$0]++;next}
            ( a[$0] >= 2 ){b[$0]++}
END {
for ( i in  b )
    if ( b >= 2)
       print i
}' f1 f2>out.dat

THANK YOU VERY MUCH FOR YOUR TIME.iT WORKS FINE.

a small corection in the input files as follows:

file1
aa
aa
aa
aa
ab
ac
ad
ae

file2
aa
ab
ac

Outputfile:
aa
aa
aa

the o/p file should get the duplicate records that are present in the file1 and not in file2

say in above ex: file 1 has aa 4 times
and file2 has aa one time

so i want out put file aa 3 times.

Use:

awk 'NR==FNR{a[$0]++;next}
            ( a[$0] >= 2 ){b[$0]++}
END {
for ( i in  b )
    if ( b == 1 )
       printf("%s\n%s\n%s\n",i,i,i)
}' f1 f2

BTW: use a more descriptive name for your posts.

You need to redirect the output to a file. It is working.

one more feature need to add to this command. example sometimes iam having files as follows.

if case1
file1
aa
aa
aa
aa
ab
ac
ad
ae

file2
aa
aa
aa
aa
ab
ac
ad
ae

Outputfile:
should be empty

if case2
file1
aa
aa
aa
aa
ab
ac
ad
ae

file2
aa
aa
ab
ac
ad
ae

Outputfile:
aa

Note: if file2 contents matches with file1 no output should be created. if file2 has records more than once, it should check that records in the file1.

in the above example. file1 has aa 4 times.

file2 has aa 2 times. then i getting the output file corrcetly
using the command given

awk 'NR==FNR{a[$0]++;next}
( a[$0] >= 2 ){b[$0]++}
END {
for ( i in b )
if ( b >= 1)
print i
}' file2 file1 > out 

but if i am having the files as in case1. then i need to get the output file empty, but using this above command iam getting output as

aa

can i get the suggesions??

help pls????????

Try:

awk 'NR==FNR{a[$0]++;next}
( a[$0] >= 2 ){b[$0]++}
END {
for ( i in b )
if ( b >= 1 && b != a )
print i
}' file2 file1>output.dat

and please do not bump questions.
Regards

It is working great....Thanks for your reply.

Sorry for creating another thread.