Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Iam having files as follows:
windows
if case1
file1
aa
aa
aa
aa
ab
ac
ad
aefile2
aa
aa
aa
aa
ab
ac
ad
aeOutputfile:
should be emptyif case2
file1
aa
aa
aa
aa
ab
ac
ad
aefile2
aa
aa
ab
ac
ad
aeOutputfile:
aaNote: 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 givenCode:
awk 'NR==FNR{a[$0]++;next}
( a[$0] >= 2 ){b[$0]++}
END {
for ( i in b )
if ( b[i] >= 1)
print i
}' file2 file1 > outbut 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 asaa
can i get the suggesions??

I do not understand your problem clearly. For this posting, I used the following rules: For records that exist 2 or more times in file2, check to see how many of those record exist in file1. If file1 has MORE of those records than file2, then print the record. If file1 has the same or LESS of those records, then do not print the record.
To make it easier for me to follow, I changed the name of your "a" array to f2 (representing file2 record counts), and the "b" array to f1 (representing file1 record counts). Also, for testing, I include the file1-count and file2-count in my print command.
awk '\
NR==FNR {f2[$0]++; next}
( f2[$0] >= 2 ) { f1[$0]++ }
END {
for ( i in f1 )
if ( f1[i] > f2[i] )
print i, f1[i], f2[i]
}' file2 file1
./myscript.sh
aa 4 2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |