user3834663
user3834663

Reputation: 543

compare two files matches a pattern and print

I have two files input1.txt and input2.txt.

input1.txt has following details:

    abcd
    bcad
    cagh
    degh
    usna

input2.txt has following details:

abcd.dev.net ip: 169.254.0.2
bcad.prod.net ip: 169.254.0.3
cagh.uat.net ip: 169.254.0.4
us-1212-qwe.net ip: 169.254.232.50
us-cisco.net ip: 10.120.2.3
degh.stage.net ip: 169.254.0.5
usna.prod.net ip: 169.254.0.6
ab1234.net ip: 169.254.0.7

- catorr.net ip: 169.254.0.8

I need to get the corresponding IP details of the servers listed in "input1.txt" from the file "input2.txt"

Output should be like this:

abcd.dev.net 169.254.0.2
bcad.prod.net 169.254.0.3
cagh.uat.net 169.254.0.4
degh.stage.net 169.254.0.5
usna.prod.net 169.254.0.6

Upvotes: 1

Views: 49

Answers (2)

justaguy
justaguy

Reputation: 3022

Would grep work:

grep -Fwf input1 input2


abcd.dev.net ip: 169.254.0.2
bcad.prod.net ip: 169.254.0.3
cagh.uat.net ip: 169.254.0.4
degh.stage.net ip: 169.254.0.5
usna.prod.net ip: 169.254.0.6

Upvotes: 1

karakfa
karakfa

Reputation: 67467

if you want to join only until the first period you can use this

$ join -t. input1 <(sort input2)

abcd.dev.net ip: 169.254.0.2
bcad.prod.net ip: 169.254.0.3
cagh.uat.net ip: 169.254.0.4
degh.stage.net ip: 169.254.0.5
usna.prod.net ip: 169.254.0.6

if input1 is not sorted, do the same <(sort input1)

Upvotes: 1

Related Questions