Computing.Net > Forums > Unix > awk command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk command

Reply to Message Icon

Name: sainivas
Date: February 26, 2008 at 02:18:27 Pacific
OS: windows 98
CPU/Ram: 1gb
Product: IBM
Comment:

Iam having files as follows:
windows
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

Code:
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 as

aa


can i get the suggesions??




Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: February 26, 2008 at 14:41:25 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk command

How to use AWK command www.computing.net/answers/unix/how-to-use-awk-command/6686.html

shortcut - awk command www.computing.net/answers/unix/shortcut-awk-command/4271.html

AWK command to find 2 values in a f www.computing.net/answers/unix/awk-command-to-find-2-values-in-a-f/5853.html