0

I would like to compare the first column of file1 with the second column of file2 and only if they match it should display any matching lines in file2 only as the output. The columns are separated by |.

file1:

syfar03040k16.audc1.oraclecloud.com |
syfar03040m02.audc1.oraclecloud.com |
syfar03040m04.audc1.oraclecloud.com |
syfar03040n11.audc1.oraclecloud.com |
syfar03040n01.audc1.oraclecloud.com |
syfar03040n02.audc1.oraclecloud.com |
syfar03040n03.audc1.oraclecloud.com |
syfar03040n05.audc1.oraclecloud.com |
syfar03040n07.audc1.oraclecloud.com |
syfar03040o11.audc1.oraclecloud.com |
syfar03040o01.audc1.oraclecloud.com |
syfar03040o02.audc1.oraclecloud.com |
syfar03040o03.audc1.oraclecloud.com |
syfar03040o13.audc1.oraclecloud.com |
syfar03040o05.audc1.oraclecloud.com |
syfar03040o04.audc1.oraclecloud.com |
syfar03040o16.audc1.oraclecloud.com |

file2:

   | LDAP                 | syfar03040o11.audc1.oraclecloud.com               |
   | OIM                  | syfar03040o01.audc1.oraclecloud.com               |
   | AUTHOHS              | syfar03040o02.audc1.oraclecloud.com               |
   | APPOHS               | syfar03040o03.audc1.oraclecloud.com               |
   | BI                   | syfar03040o04.audc1.oraclecloud.com               |
   | ADMIN                | syfar03040o05.audc1.oraclecloud.com               |
   | PRIMARY              | syfar03040o06.audc1.oraclecloud.com               |
   | SECONDARY            | syfar03040o07.audc1.oraclecloud.com               |
   | APPOHS_HA1           | syfar03040o13.audc1.oraclecloud.com               |
   | PRIMARY_HA1          | syfar03040o16.audc1.oraclecloud.com               |
   | SECONDARY_HA1        | syfar03040o17.audc1.oraclecloud.com               |
   | OSN                  | syfar03040o09.audc1.oraclecloud.com               |

File3:

   | LDAP                 | syfar03040o11.audc1.oraclecloud.com               |
   | OIM                  | syfar03040o01.audc1.oraclecloud.com               |
   | AUTHOHS              | syfar03040o02.audc1.oraclecloud.com               |
   | APPOHS               | syfar03040o03.audc1.oraclecloud.com               |
   | BI                   | syfar03040o04.audc1.oraclecloud.com               |
   | ADMIN                | syfar03040o05.audc1.oraclecloud.com               |
   | APPOHS_HA1           | syfar03040o13.audc1.oraclecloud.com               |
   | PRIMARY_HA1          | syfar03040o16.audc1.oraclecloud.com               |

marked as duplicate by jasonwryan, Wildcard, MelBurslan, Jakuje, Jeff Schaller Apr 4 '16 at 19:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I simplified your data to make your question more understandable. If I seriously changed the meaning, edit it yourself and fix it. – G-Man Apr 4 '16 at 18:48
1
awk '
    NR == FNR {
        file1[$1] = 1;
        next;
    }
    $4 in file1 {
        print $0;
    }
' file1 file2
  • Thank You. But the code does not seem to work with my original files. There is no output displayed. I initially tried awk -F"|" 'NR==FNR{a[$1]=1;next} a[$4] {print $0}' file1 file2 but with this it just display the entire file 2 which is not my requirement ----------------------------------------------------------------------------- For reference again, Here are my original files- – neha dhawan Apr 5 '16 at 1:38
  • I tried this code and it worked awk ' > FNR == NR { > a[ $1 ] = 1; > next; > } > FNR < NR { > if ( ($4 in a) ) { > print $0; > } > } > ' file1 file2 .......... – neha dhawan Apr 5 '16 at 1:58
  • @nehadhawan Yes I see, there was a mistake with the file delimiters in my answer. I updated it; also, the FNR < NR test in your code can be left away. FNR is the current record number in the current file and is reset to 0 when the next file is read, while NR is the total number of records. So after the first file has been read, FNR < NR will always be true. – Guido Apr 5 '16 at 11:06

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