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文本进行自我的体验。

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

相关推荐
曹牧20 小时前
Oracle:xml转义
xml·数据库·oracle
曹牧21 小时前
Java:XML转义
xml·java·开发语言
摇滚侠1 天前
Spring 零基础入门到进阶 基于注解管理 Bean 38-43
xml·java·后端·spring·intellij-idea
San813_LDD2 天前
[深度学习] 数据序列化格式对比:以日志级别配置为例
xml·java·前端
摇滚侠2 天前
Spring 零基础入门到进阶 基于 XML 的声明式事务 71
xml·数据库·spring
摇滚侠3 天前
Spring 零基础入门到进阶 基于 XML 管理 Bean 14-28
xml·数据库·spring
摇滚侠3 天前
Spring 零基础入门到进阶 基于 XML 管理 Bean 29-37
xml·java·数据库·后端·spring·intellij-idea
hashiqimiya4 天前
每日android布局xml文件
android·xml·gitee
2601_961194024 天前
27考研资料|百度网盘|夸克网盘
android·xml·考研·ios·iphone·xcode·webview
许彰午7 天前
在PowerBuilder里手写XML序列化——没有现成库的年代怎么拼报文
xml·linux·服务器