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!

Compare two files 2

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

How can I compare two files for eg if line in filea is not in fileb print "filea Diff in" and if line in fileb is not in filea print "fileb Diff in". The below code will work if I put fileb as the first file and filea as the second

awk '
NR==FNR{a[$0];next}
{if (!($0 in a)){
print "Diff in =====>",$0
}
}
' filea fileb

Thanks
Chris
 
Why not using the standard diff command ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

Just to see if, and I now that awk can do the trick.

Thanks Once Again
Chris
 
A starting point:
awk '
NR==FNR{a[$0];next}
$0 in a{delete a[$0];next}
{print "in b",$0}
END{for(i in a)print "in a",i}
' filea fileb


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
{ lines[ $0 ]
  files[ $0, FILENAME ]
}
END {
  FileCount = ARGC - 1
  for (line in lines)
  { count = 0;   stats = ""
    for (i=1; i<ARGC; i++)
    { if ( (line, ARGV[i]) in files )
      { stats = stats i % 10
        count++
      }
      else
        stats = stats " "
    }
    if ( count < FileCount )
      print stats "  " line
  }
  print "--\nFiles:"
  for (i=1; i<ARGC; i++)
    printf "%2d. %s\n", i, ARGV[i]
}
File junk:
[tt]one
two
three
four
foo
bar
baz
quux
[/tt]
File junk2:
[tt]one
two
three
four
five
six
[/tt]
File junk3:
[tt]Surprise!
two
three
four
foo
bar
baz
[/tt]
Output:
Code:
1 3  baz
1    quux
1 3  foo
12   one
  3  Surprise!
 2   six
 2   five
1 3  bar
--
Files:
 1. junk
 2. junk2
 3. junk3
 
Hi PHV,

As usual spot on. One question, delete a[$0]???, could you please explain. Futurelet looks good will try it.

Once Again Many Thanks
Chris
 
delete a[$0]
Remove remembered fileA's line when seen in fileB

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