[英]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: 循环而不是单循环,但是当
f1
和f2
是两个/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.