我有一个分布式网络监视系统的综合主机文件,我需要将其分割为两个文件。一些主机进入一个文件,其余的进入另一个文件。我正在使用sed
来拉出我需要的主机,但是我不知道如何将其他所有文件都放在其他文件中,比如选项grep
。主机文件的选择除文本块外的所有内容
例子:
object Host "router" {
import "template"
display_name = "router"
address = "xx.xx.xx.xx "
}
object Host "switch" {
import "template"
display_name = "switch"
address = "xx.xx.xx.xx "
}
object Host "router" {
import "template"
display_name = "router"
address = "xx.xx.xx.xx "
}
object Host "otherthing" {
import "template"
display_name = "otherthing"
address = "xx.xx.xx.xx "
}
object Host "switch" {
import "template"
display_name = "switch"
address = "xx.xx.xx.xx "
}
object Host "otherthing" {
import "template"
display_name = "otherthing"
address = "xx.xx.xx.xx "
}
的sed
命令我使用的是拉出来的路由器
sed '/router.*\n/p;//g;/{$/,/^}/H;//x;D' files/tmp/unsorted-host.tmp >> files/router.conf
此外,我不知道这是否会更有效率,而不是排序建立大型主机文件,但这里是我如何使大型主机文件:
while read line
do
# The SNMP to check the device type outputs on multiple lines, check to ensure we only grab IP's
getip=$(echo $line | awk '{print $1}')
if ping -c 1 $getip &> /dev/null
then
ip=$getip
else
trash=$getip
fi
template="template"
host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community1 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}')
if [ -z "$host_name" ]
then
host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community2 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}')
if [ -z "$host_name" ]
then
host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community3 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}')
if [ -z "$host_name" ]
then
host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community4 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}')
if [ -z "$host_name" ]
then
host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community5 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}')
if [ -z "$host_name" ]
then
host_name=$(echo " Not found ")
fi
fi
fi
fi
fi
echo "
object Host $host_name {
import \"$template\"
display_name = $host_name
address = \"$ip \"
}"
done <files/tmp/hosts.tmp> files/tmp/unsorted-hosts.tmp
目标是有两个文件,一个是路由器,另一个是其他所有文件。提前感谢任何和所有的建议!
我没有详细看过你的'sed'语句,但是如果你能用'sed'找到你需要的所有东西,那么你不能只是做一个' sed'替换删除路由器材料? – TTT