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 IamaSherpa 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 and delete records matching in the first file

Status
Not open for further replies.

chapm4

IS-IT--Management
Nov 12, 2001
38
US
Think awk may be the solution but not sure.

Example:

File 1 File 2
M02464217A M03234567A
M01234567A M02464217A
M02345678A
M03456789A

If data in File 2 matches data in File 1, I want to remove matching data from File 1. In above example I would remove M03234567A from File 1.


 
This works, but it's ugly.

awk ' {
while ((getline I < &quot;target&quot;) > )) {
if ($0 == I) {
print $0 >> targetfile
next
}
}
}'
 
Hi chapm4,

I assume you want M02464217A deleted from the first file. If not you can change this program to do what you want. Run it by entering

awk -f awkscript file1 > file3

Code:
BEGIN {
  while ((getline < &quot;file2&quot;) > 0) {
    hld[++ix] = $0
  }
}
{
  flg = 0
  for (i=1; i<=ix && !flg; i++) {
    if ($0 == hld[i]) flg = 1
  }
  if (!flg) print
}
Hope this helps. CaKiwi
 
typo:
> 0 not > )

Problem:
you still need to write to a temp file and remove the original this way.
tried also:
for xx in $(cat file)
do
if grep $xx secondfile
then
sed &quot; s/$xx// &quot; file >> holdfile
fi
done
 
or you could change it in place by modifying marsd's slolution to use ex
Code:
for xx in `cat file2`
do
  echo &quot;g/$xx/d|w&quot; | ex - file1
done
CaKiwi
 
If input files are sorted (it's not your case) you can use the join command.

join -v1 file1 file2 > file3

In this simple example, join compare the first field of lines of each files and output unpairables lines in file1. Jean Pierre.
 
Another version of CaKiwi solution :

awk -f awkscript file1 > file3

BEGIN {
while ((getline < &quot;file2&quot;) > 0) {
hld[$0] = 1 ;
}
}
{
if (! hld[$0]) print $0
}

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top