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

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

相关推荐
spencer_tseng1 天前
[j2cache ehcache.xml]/tmp/.ehcache-diskstore.lock (Permission denied)
xml·linux·python·ehcache·[j2cache
DevOpenClub2 天前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
Java小白笔记2 天前
MyBatis XML Mapper 标签与 CRUD 原理实战
xml·数据库·mybatis
风萧萧19993 天前
JAVA :JSONObject转换为XML
xml·java·python
马优晨5 天前
pom.xml 中 jquery webjars 依赖解释
xml·前端·jquery·jquery webjars·webjars依赖
楚识科技6 天前
定制 OCR 服务:非标证件与表格的 XML 字段映射实践
xml·ocr
光泽雨7 天前
JSON /XML转换
xml·json
我命由我123458 天前
UI 设计 7 种排列方式
xml·学习·ui·html·产品运营·产品经理·学习方法
ly768913 天前
XML 从入门到实践:语法、命名空间、XPath、XSD 与 Java 安全解析
xml·java·python