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

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

相关推荐
泡海椒14 小时前
JQuick-Excel JQuickRow 数据准备实战:JObjectConverter、JQuickRow.toRows 与 XML 导出链路
xml·windows·excel
泡海椒3 天前
JQuick-Excel 字段映射实战:用 MAPPING 固化 Excel 表头与业务字段契约
xml·java·开发语言·excel
执明wa15 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
wl851116 天前
SAP CPI 教程004 如何通过groovy修改XML节点属性值
xml
码匠许师傅17 天前
【C++三方组件】TinyXML2:两个文件的极简 XML 解析器
xml·c++
yuzhiboyouye18 天前
my-batisPlus什么时候用xml什么时候用mapper自带的方法,这两种写法专业一点叫什么,什么场景分别使用
xml
筝筝ba19 天前
如何跳过FFDC
xml·linux·运维·服务器·网络
yuzhiboyouye20 天前
那xml对应的sql语法,列举一下
xml·数据库·sql
筝筝ba25 天前
OpenBMC 自动化测试中的多进程陷阱:从 Execute Process 返回异常到 output.xml 损坏问题分析
xml·linux·运维·服务器·网络
spencer_tseng1 个月前
[j2cache ehcache.xml]/tmp/.ehcache-diskstore.lock (Permission denied)
xml·linux·python·ehcache·[j2cache