我正在通过脚本从数千个os服务器中提取NTP记录,列表如下所示。
包含服务器名称和NTP服务器的文件:
dbfx9005
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
server sas1.ntp.gus.com iburst
dbfx9007
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
dbfx9008
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
server sas.ntp.gus.com iburst我想做的是:
dbfx9005和dbfx9007是服务器名称,以server开头的任何内容都是NTP服务器。我期待着一种方法来验证它下面的每个服务器是否包含在三个NTP服务器下面。
sas.ntp.gus.com
sas2.ntp.gus.com
sas1.ntp.gus.com如果其中任何一个不是这些,那么就像sas2.ntp.gus.com一样报告它,而不是在dbfx9005中报告。
编辑:,我希望把这三个名字命名为sas.ntp.gus.com , sas2.ntp.gus.com and sas1.ntp.gus.com,但是如果他们中的任何一个没有重复,那么只需打印它们,只显示一个服务器名。
我对Shell、Python或Pandas的任何想法都没意见。
谢谢你们的帮助。
发布于 2021-03-19 09:37:43
利用GNU awk:
awk '!/ntp/ {srv=$0;next } /sas((1)|(2))?.ntp.[[:alnum:]]+.[[:alnum:]]+/ { map[srv][$2]="1" } END { for (i in map) { if (map[i]["sas.ntp.gus.com"]!="") { print i" - sas.ntp.gus.com exists" } else { print i" - sas.ntp.gus.com DOESNT exist" }; if (map[i]["sas1.ntp.gus.com"]!="") { print i" - sas1.ntp.gus.com exists" } else { print i" - sas1.ntp.gus.com DOESNT exist" };if (map[i]["sas2.ntp.gus.com"]!="") { print i" - sas2.ntp.gus.com exists" } else { print i" - sas2.ntp.gus.com DOESNT exist" }; }}' file解释:
awk '!/ntp/ {
srv=$0; # For lines not containing "ntp" track the server name in the variable srv
next
}
/sas((1)|(2))?.ntp.[[:alnum:]]+.[[:alnum:]]+/ {
map[srv][$2]="1" # For ntp lines, use a 2 dimensional array with srv the first index and the ntp server address the second index
}
END {
for (i in map) { # At the end of processing the file, loop through the servers in the array map
if (map[i]["sas.ntp.gus.com"]!="") {
print i" - sas.ntp.gus.com exists" # If a second index exists for "sas.ntp.gus.com", print that it exists, otherwise print that it doesn't
}
else {
print i" - sas.ntp.gus.com DOESNT exist"
};
if (map[i]["sas1.ntp.gus.com"]!="") {
print i" - sas1.ntp.gus.com exists" # Follow the same logic for sas1 that was followed for sas
}
else {
print i" - sas1.ntp.gus.com DOESNT exist"
};
if (map[i]["sas2.ntp.gus.com"]!="") {
print i" - sas2.ntp.gus.com exists" # Follow the same logic for sas2 that was used for sas
}
else {
print i" - sas2.ntp.gus.com DOESNT exist"
};
}
}' file输出:
dbfx9005 - sas.ntp.gus.com exists
dbfx9005 - sas1.ntp.gus.com exists
dbfx9005 - sas2.ntp.gus.com exists
dbfx9007 - sas.ntp.gus.com exists
dbfx9007 - sas1.ntp.gus.com DOESNT exist
dbfx9007 - sas2.ntp.gus.com exists
dbfx9008 - sas.ntp.gus.com exists
dbfx9008 - sas1.ntp.gus.com DOESNT exist
dbfx9008 - sas2.ntp.gus.com existshttps://stackoverflow.com/questions/66705518
复制相似问题