0

I need to filter out records in the second file:

Let's say the file1 is like so:

0006296 OIL FILTER PH3506   HONEYWELL   Parts and Repair    3101
0015735 OIL FILTER PH2870   HONEYWELL   Parts and Repair    3101

and file2 will be:

0006296 OIL FILTER PH3506   HONEYWELL   Parts and Repair    3101
0015735 OIL FILTER PH2870   HONEYWELL   Automotive          4201

how would I filter out the second row in file 2, based on the fact it has a matching first column but a column that doesn't match as in the last column.

4
  • 2
    Is there a 1:1 correspondence of lines in file 1 and file 2? That is to say, e.g., that line 2 is the same line in both files just with possibly different values in the last column? Or are there lines in file 1 not present in file 2, or vice-versa? – KevinO Jun 16 '17 at 19:43
  • 1
    can you add more context (more lines) and expected result? – RomanPerekhrest Jun 16 '17 at 19:52
  • Kevin, both files will have the same number of columns. Roman, expected result would be to have the the first line only appear in the new file, as the second line shows a mismatch. – user236443 Jun 16 '17 at 20:01
  • KevinO's question is whether the files have matching lines. How do you know that line 2 of file2 corresponds to line 1 of file1? Is it because of the line numbers, or because one of the columns (which one) has the same content? What do you do when a line in file2 doesn't match any of the lines in file1? – Gilles 'SO- stop being evil' Jun 16 '17 at 21:57
0

This should be enough using as key the first and the last field:

$ awk 'NR==FNR{a[$1"-"$NF]++;next}$1"-"$NF in a' file1 file2
0006296 OIL FILTER PH3506   HONEYWELL   Parts and Repair    3101

We are separating those two fields with something (a dash in my example) to avoid confusion of 0006296 and 3101 with 00062963 and 101

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.