shell解决xml文本中筛选的问题

shell解决xml文本中筛选的问题,将xml文件中多余的配置删除掉,达到符合单一配置的结果。

过于简单就不废话了。

#!/bin/bash

conf_file=$1
flag=0
temp_file="__temp.xml"

if [ -f "${temp_file}" ];then
    rm "${temp_file}"
fi
while read line
do
    echo "${line}" | grep '<connection' > /dev/null
    if [ $? = 0 ];then
        flag=1
        echo "${line}"
        echo "${line}" >> "${temp_file}"
        continue
    fi
    echo "${line}" | grep '</connection' > /dev/null
    if [ $? = 0 ];then
        flag=0
        echo "${line}"
        echo "${line}" >> "${temp_file}"
        continue
    fi
    echo "${line}" | grep '<server' > /dev/null
    if [ $? = 0 -a ${flag} = 1 ];then
        flag=2
        echo "${line}"
        echo "${line}" >> "${temp_file}"
        continue
    elif [ $? = 0 -a ${flag} = 2 ];then
        continue
    fi
    if [ ${flag} = 0 ];then
        echo ${line} >> "${temp_file}"
    fi
done < ${conf_file}

然而在其他的解决方式中,也可以通过awk脚本直接将结果进行统一的处理

比如:

awk '!/<server ip=/{print;c=0}   /<server ip=/ && ++c==1{print}' xxx.xml > __temp.xml

可以自己找一个xml文本进行自我的体验。

真正的结果一定要自己体验,否则就没有意义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值