段落转换(给正文行首加上段落首行)shell

2019-03-19  本文已影响0人  六十三63

每一次文本处理的背后都是无聊的灵魂

标准不统一的问题给人造成的困扰很大

比如网络给你的是他交换机的信息,你要看的却是你物理服务器的信息。

交换机信息的信息如下

aaa_leaf_1_H777     
port        device
10GB/0/1        host1
10GB/0/2        host2
10GB/0/3        host3
10GB/0/4        ERROR
10GB/0/5        host5
10GB/0/6        host6
10GB/0/7        host7
10GB/0/8        ERROR
10GB/0/9        ERROR
10GB/0/10       host10
10GB/0/11       host11
10GB/0/12       ERROR
10GB/0/13       host13
10GB/0/14       host14
        
abb_leaf_1_H777     
port        device
10GB/0/1        host18
10GB/0/2        host19
10GB/0/3        host20
10GB/0/4        host21
10GB/0/5        host22
10GB/0/6        host23
10GB/0/7        host24
10GB/0/8        host25
10GB/0/9        host26
10GB/0/10       host27
10GB/0/11       host28
10GB/0/12       host29
10GB/0/13       host30
10GB/0/14       host31
        
ccc_leaf_1_H777     
port        device
10GB/0/1        host18
10GB/0/2        host19
10GB/0/3        host20
10GB/0/4        host21
10GB/0/5        ERROR
10GB/0/6        ERROR
10GB/0/7        ERROR
10GB/0/8        host25
10GB/0/9        host26
10GB/0/10       host27
10GB/0/11       host28
10GB/0/12       ERROR
10GB/0/13       host30
10GB/0/14       host31

但是我主机端的表是这样

host1   aaa_leaf_1#1    ccc_leaf_1#1
host2   aaa_leaf_1#2    ccc_leaf_1#2
host3   aaa_leaf_1#3    ccc_leaf_1#3
host4   aaa_leaf_1#4    ccc_leaf_1#4
host5   aaa_leaf_1#5    ccc_leaf_1#5
host6   aaa_leaf_1#6    ccc_leaf_1#6
host7   aaa_leaf_1#7    ccc_leaf_1#7
host8   aaa_leaf_1#8    ccc_leaf_1#8
host9   aaa_leaf_1#9    ccc_leaf_1#9
host10  aaa_leaf_1#10   ccc_leaf_1#10
host11  aaa_leaf_1#11   ccc_leaf_1#11
host12  aaa_leaf_1#12   ccc_leaf_1#12
host13  aaa_leaf_1#13   ccc_leaf_1#13
host14  aaa_leaf_1#14   ccc_leaf_1#14
host15  aaa_leaf_1#15   ccc_leaf_1#15
host16  aaa_leaf_1#16   ccc_leaf_1#16
host17  aaa_leaf_1#17   ccc_leaf_1#17
host18  aaa_leaf_1#18   ccc_leaf_1#18

这让我怎么让快熟找出我哪些主机设置的LLDP有问题呢,只能先处理交换机端出现的信息

以下是研究出来的函数,当然后期还要加工以下,但已经能把每个段落中端口信息加上段落的首行,也就是交换机信息

switch_ch () {
OLDFILE=$1
cat $OLDFILE|sed '/^\t/d'|grep -v ^port>newfile
TARGERFILE=newfile
SWITCHLINELIST=(`cat -n $TARGERFILE|grep 'leaf'|awk '{print $1}'`)
SWITCHNAMELIT=(`cat -n $TARGERFILE|grep 'leaf'|awk '{print $2}'`)
i=0
FILEENDLINE=`cat $TARGERFILE|wc -l`
ENDINDEX=`expr ${#SWITCHLINELIST[@]} - 1`
while [ $i -le $ENDINDEX ]
do
STARLINE=`expr ${SWITCHLINELIST[$i]} + 1`
LEAFNAME=`echo ${SWITCHNAMELIT[$i]}|awk -F'_' '{print $1"_"$2"_"$3}'`
if [ $i -eq $ENDINDEX ];then
sed -i "${STARLINE},${FILEENDLINE}s/^/${LEAFNAME}/g" $TARGERFILE
else
NEXTLEAFLINEINDEX=`expr $i + 1`
NEXTLEAFLINE=`expr ${SWITCHLINELIST[$NEXTLEAFLINEINDEX]} - 1`
sed -i "${STARLINE},${NEXTLEAFLINE}s/^/${LEAFNAME}/g" $TARGERFILE
fi
i=`expr $i + 1`
done
}
switch_ch
上一篇下一篇

猜你喜欢

热点阅读