Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing columns in 2 files 1

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
I have file1:

Code:
foo:user1:foo
foo:user4:foo
foo:user2:foo
foo:user6:foo

file2:
Code:
user2
user3
user1

Desired output:
Code:
user2 exists in file1
user3 does NOT exist in file1
user1 exists in file

Any way to do this with awk?

TIA


 
Code:
awk -F: '
BEGIN {
        # Read file1 into an array
        while (getline < "file1") {
                users[$2]=1
        }
}
{
        # For every line in file2, test for presence and
        # display result
        if (users[$1])
                print $1 " exists in file1"
        else
                print $1 " does NOT exist in file1"
}
' file2

Annihilannic.
 
awk -F: 'NR==FNR{a[$2];next}{print $1 ($1 in a ? " exists" : " does NOT exist") " in file1"}' file1 file2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top