主机文件为所有服务器创建唯一文件 [英] Host file make a unique file for all servers

查看:95
本文介绍了主机文件为所有服务器创建唯一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多主机文件.我从所有服务器上收集它们,并将它们放到host_files.txt中,然后必须为所有服务器制作一个主机文件.

I have many hosts files. I collect them from all servers and i put them together in host_files.txt and then I must make one hosts file for all servers.

我执行此命令以创建唯一文件,但是某些行共享相同的IP地址或主机名.

I do this command to make a unique file, but some rows share the same ip address or hostname.

awk '!a[$0]++' host_files.txt

这是我的host_files.txt

Here is my host_files.txt

#backup server IPs
95.23.23.56
95.23.23.57 

#ftp server IPs
45.89.67.5 
45.89.67.3 

#apache
12.56.35.36 
12.56.35.35 

#ftp server IPs
95.23.23.50

#apache
12.56.35.37 

我想输出文件,但是我需要保留注释行

I want to output file, but I need to keep the comment line

#backup server IPs <= comment line, i need to keep them
95.23.23.56 
95.23.23.57 

#ftp server IPs <= comment line, i need to keep them
45.89.67.5 
45.89.67.3 
95.23.23.50

#apache <= comment line, i need to keep them
12.56.35.36
12.56.35.35 
12.56.35.37

我已经尝试过:

sort -ur host_files.txt

cat host_files.txt | uniq > ok_host.txt

我需要没有#的IP,只需要ip地址,请帮助我

I need the ip without # just need ip adresse please help me

预先感谢

推荐答案

这将在任何awk中起作用:

This will work in any awk:

$ cat tst.awk
/^#/ { key = $0; next }
NF && !seen[$0]++ {
    ips[key] = ips[key] $0 ORS
}
END {
    for (key in ips) {
        print key ORS ips[key]
    }
}

$ awk -f tst.awk file
#apache
12.56.35.36 #apachepub
12.56.35.35 #apacheprivate
12.56.35.37 #apachepub

#ftp server IPs
45.89.67.5 #ftpmain
45.89.67.3 #ftpssh
95.23.23.50 #ftp

#backup server IPs
95.23.23.56 #masterbasckup
95.23.23.57 #agentbasckup

由于使用了in运算符,输出顺序将是随机的,如果存在问题,只需更改几行代码即可.

Output order will be random due to use of the in operator, if that's a problem it's just a couple more lines of code to change.

这篇关于主机文件为所有服务器创建唯一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆