【问题标题】:compare two files matches a pattern and print比较两个文件匹配一个模式并打印
【发布时间】:2016-03-16 02:12:39
【问题描述】:

我有两个文件 input1.txt 和 input2.txt。

input1.txt 有以下详细信息:

    abcd
    bcad
    cagh
    degh
    usna

input2.txt 有以下详细信息:

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

我需要从文件“input2.txt”中获取“input1.txt”中列出的服务器对应的IP详细信息

输出应该是这样的:

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

【问题讨论】:

  • 这不是“为我做这个”网站,向我们展示您的尝试和问题
  • 想知道这真的不可能使用 awk。

标签: awk


【解决方案1】:

如果您只想在第一个期间加入,您可以使用它

$ 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

如果 input1 没有排序,同样的操作&lt;(sort input1)

【讨论】:

    【解决方案2】:

    grep 会工作吗:

    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
    

    【讨论】:

    • 请注意,这将匹配任何匹配项,不一定是预期位置。
    • 这是真的,好点 :).... 使 grep 选项更好,我希望 :) 谢谢
    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 2016-10-23
    • 2012-09-05
    • 2016-07-30
    • 2017-07-07
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多