简体   繁体   English

如果IP相同,则合并两个/ etc / hosts文件

[英]Merge two /etc/hosts files if IP as the same

I need merge two /etc/hosts files in unique file only if the IP adress is the same. 仅当IP地址相同时,才需要将两个/ etc / hosts文件合并到唯一文件中。

File 1 contains: 文件1包含:

172.27.88.143   node1
172.23.171.42   node2
172.23.171.36   node3
172.27.88.136   node4
172.27.88.137   node5
172.27.88.138   node5
172.27.88.200   node6

File 2 contain: 文件2包含:

172.27.88.200   node6.domain.corp
172.27.88.158   node7.domain.corp

The result file must be: 结果文件必须是:

172.27.88.143   node1
172.23.171.42   node2
172.23.171.36   node3
172.27.88.136   node4
172.27.88.137   node5
172.27.88.138   node5
172.27.88.200   node6   node6.domain.corp

I need to it on Linux systems. 我在Linux系统上需要它。 That's possible? 有可能吗

Thanks in advace for your time 感谢您的抽空

跟随awk可能会对您有所帮助。

awk 'FNR==NR{a[$1]=$2;next} {print $0,a[$1]?"\t" a[$1]:a[$1]}' FILE2 FILE1

A loop rather than a one-liner, but this should work when f1 and f2 are variables for the two /etc/hosts files: 循环而不是单循环,但是当f1f2是两个/etc/hosts文件的变量时,这应该起作用:

while read line; do
    ip="$(echo "${line}" | awk '{print $2}')"
    secondName="$(grep "${ip}" "${f2}" | awk '{print $2}')"

    echo "${line} ${secondName}"
done < "${f1}"

The output contains some trailing space, which can be removed by piping the echo-line through | sed 's/ \\+$//' 输出中包含一些尾随空格,可通过将回声线通过| sed 's/ \\+$//'来消除| sed 's/ \\+$//' | sed 's/ \\+$//' . | sed 's/ \\+$//'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 /etc/hosts 中引用具有不同规范或别名的相同 IP - Referring same IP with different canonical or alias name in /etc/hosts / etc / hosts linux文件指向两个地址(本地和Internet IP) - /etc/hosts linux file pointing two address (local and internet IP) 需要使用已知主机名grep / etc / hosts,然后从/ etc / hosts捕获主机名的ip地址 - Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts 如何让curl使用指定的ip而不是在/ etc / hosts中设置它? - How to make curl use specified ip instead of setting it in /etc/hosts? Linux-在两个主机之间转发IP数据包 - linux - forward ip packets between two hosts 使用 fetchFromGithub 从 master 分支获取 hosts 文件并将其合并到 /etc/hosts - Use fetchFromGithub to fetch hosts file from master branch and merge it to /etc/hosts 合并两个文件夹并保持文件具有相同的名称 - merge two folders and keeping the files have same name 合并两个文件 AWK - Merge two files AWK 如果服务器不用作邮件服务器,则将“ IP domain.tld”添加到(/ etc / hosts)文件有什么意义? - What is the point of adding “IP domain.tld” to the ( /etc/hosts ) file if the server is not gonna be used as a mail server? 1 Liner Script获取服务器的IP地址(ifconfig)并将其附加到/ etc / hosts的HostName中以匹配ipaddress - 1 Liner Script to get IP Address of server (ifconfig) and append to HostName to /etc/hosts for matching ipaddress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM