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!

search file1 rows for file2 rows, if yes then remove rows 1

Status
Not open for further replies.

dunk2

Technical User
Jun 10, 2002
14
IE
Hi,

I a large file1. I want to search it for lines that contain the same value in file2. If the values match then remove the row from file 1.
Example of file1
5937000862 |414160|"SP"|"S"|"PRUEBAS"||20011025|+0000024.13|20011024|932
5937000000 |696781|"SP"|"A"|"JACQUELINE JANET"|20020503|20020704|+0000000.52|20020824|942

Example of file2
5937000862

I've used the following script, but get errors

BEGIN {
while ((getline < &quot;file1&quot;) > 0) {
array[$1]= $1
}
}
{
for (i in array) {
if ($1 == i) {
&quot;&quot; = array
}
}
print
}' file2

*********
errors returned;
[larson tecnomen ] /export/home/migration/revC> ./NoFailScript3
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 7
awk: bailing out near line 7
 
Your post got messed up because the was interpreted as turning on italics. Here's my solution.
awk 'BEGIN {
while ((getline < &quot;file1&quot;) > 0) array[$1]=1
}
{
if (!array[$1]) print
}' file2

If you are on Solaris you will need to use nawk. CaKiwi
 
try a simple good old and FASTER 'sed'

a) construct from file2 a sed-script
sed -e '/^$/d ; s/.*/s\/&\/\/g/' file2 >file4sed

in file4sed you find:
s/5937000862//g
this DO match: aaa5937000862aaa

or (using the other sed below)
s/\<5937000862\>//g
this DO NOT match: aaa5937000862aaa

/^$/d purges empty lines

b) run the created script on your datafile
sed -f file4sed file1 > output

if you need word-match
sed -e '/^$/d ; s/.*/s\/\\\<&\\\>\/\/g/' file2 >file4sed
 
If your file2 lines can match anywhere in file1, the following code should work:
Code:
grep -v -f file2 file1

If you want word match or start of line match, use jamisar solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top