Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus,

I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite comfortable with awk, sed and perl. Please help me out. Your time is really appreciated.

Ex.
file1:

81457
742222
1089361
1098187
....

file2:

1,81457,81457,1,2,0,947,
1,742222,742222,1,2,0,2994,
1,555555555,555555555,1,2,0,1,
1,796224447,796224447,1,2,0,1,
.....

In this ex. say I want to delete lines 1 and 2 from file2 since the patterns '81457', '742222' are there in file1.

Thanks,
Toms

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 08:31 AM ---------- Previous update was at 08:28 AM ----------

nawk '
  FNR==NR {a[$0];next}
  { for(i in a) if (i ~ $0) next}
  1' file1 file2

i found a perl onliner for the same , take care as it edits the original file.

TESTBOX>for i in `cat file1`
 do
 perl -ni -e "print unless /$i/" file2
 done

perl:

my $reg=qr/,(81457|742222|1089361|1098187),/;
while(<DATA>){
	print if not /$reg/;
}
__DATA__
1,81457,81457,1,2,0,947,
1,742222,742222,1,2,0,2994,
1,555555555,555555555,1,2,0,1,
1,796224447,796224447,1,2,0,1,

Thanks a lot... Saved a lot of time....