使用纯 bash 解析 xml

使用纯 bash 解析 xml

我需要解析没有 的 XML xmlparser,这可能吗?我从 开始xmlstarlet,它工作得很好,但我不能在那台机器上使用任何真正的解析器。

我需要它成为一句单行话。我只需要这个:

/dev/mdX /dev/sda5 /dev/sdma6 /dev/sdmb6 ... /dev/sdsb5

我的脚本目前看起来像这样:

cat xml.txt; j=!:1; \
for i in $( xmlstarlet sel -t -v '/spaces/space/device/lvm/raids/raid/@path' $j ); \
do echo -e \\n$i $( sed -n 's/[^\"]*\"\([^\"]*\)\"[^\"]*/\1 /gp' $j | cut -d " " -f2); \
done

其输出到

/dev/md2 UTF-8 /volume1 X3BAVZ-PqyQ-BNvJ-mYaf-9lAR-Up24-icJSsM 11a49b00:599c89fe:09b6521c:2cea662f /dev/sdma6 /dev/sdmb6
/dev/sdmc6 /dev/sdna6 /dev/sdnb6 /dev/sdnc6 /dev/sdoa6 /dev/sdob6 /dev/sdoc6 /dev/sdpa6 /dev/sdpb6 /dev/sdqb6 /dev/sdqc6
/dev/sdra6 /dev/sdrb6 /dev/sdrc6 /dev/sdsa6 /dev/sdsb6 a11476dd:d11720f9:3ae54a77:a2fdb1eb /dev/sda5 /dev/sdb5
/dev/sdc5 /dev/sdd5 /dev/sde5 /dev/sdf5 /dev/sdg5 /dev/sdh5 /dev/sdi5 /dev/sdj5 /dev/sdma5 /dev/sdmb5 /dev/sdmc5 /dev/sdna5
/dev/sdnb5 /dev/sdnc5 /dev/sdoa5 /dev/sdob5 /dev/sdoc5 /dev/sdpa5 /dev/sdpb5 /dev/sdqb5 /dev/sdqc5 /dev/sdra5 /dev/sdrb5
/dev/sdrc5 /dev/sdsa5 /dev/sdsb5 /dev/vg1000/lv

或者当我也在第二部分中使用解析器时,这正是我所需要的,但我需要消除xmlstarlet

cat xml.txt; j=!:1;
for i in $( xmlstarlet sel -t -v '/spaces/space/device/lvm/raids/raid/@path' $j );
do echo -e \\n$i $( xmlstarlet sel -t -v '/spaces/space/device/lvm/raids/raid/disks/disk/@dev_path' $j );
done

输出:

/dev/md3 /dev/sdma6 /dev/sdmb6 /dev/sdmc6 /dev/sdna6 /dev/sdnb6 /dev/sdnc6 /dev/sdoa6 /dev/sdob6 /dev/sdoc6 /dev/sdpa6 /dev/sdpb6 /dev/sdqb6 /dev/sdqc6 /dev/sdra6 /dev/sdrb6 /dev/sdrc6 /dev/sdsa6 /dev/sdsb6 /dev/sda5 /dev/sdb5 /dev/sdc5 /dev/sdd5 /dev/sde5 /dev/sdf5 /dev/sdg5 /dev/sdh5 /dev/sdi5 /dev/sdj5 /dev/sdma5 /dev/sdmb5 /dev/sdmc5 /dev/sdna5 /dev/sdnb5 /dev/sdnc5 /dev/sdoa5 /dev/sdob5 /dev/sdoc5 /dev/sdpa5 /dev/sdpb5 /dev/sdqb5 /dev/sdqc5 /dev/sdra5 /dev/sdrb5 /dev/sdrc5 /dev/sdsa5 /dev/sdsb5

/dev/md2 /dev/sdma6 /dev/sdmb6 /dev/sdmc6 /dev/sdna6 /dev/sdnb6 /dev/sdnc6 /dev/sdoa6 /dev/sdob6 /dev/sdoc6 /dev/sdpa6 /dev/sdpb6 /dev/sdqb6 /dev/sdqc6 /dev/sdra6 /dev/sdrb6 /dev/sdrc6 /dev/sdsa6 /dev/sdsb6 /dev/sda5 /dev/sdb5 /dev/sdc5 /dev/sdd5 /dev/sde5 /dev/sdf5 /dev/sdg5 /dev/sdh5 /dev/sdi5 /dev/sdj5 /dev/sdma5 /dev/sdmb5 /dev/sdmc5 /dev/sdna5 /dev/sdnb5 /dev/sdnc5 /dev/sdoa5 /dev/sdob5 /dev/sdoc5 /dev/sdpa5 /dev/sdpb5 /dev/sdqb5 /dev/sdqc5 /dev/sdra5 /dev/sdrb5 /dev/sdrc5 /dev/sdsa5 /dev/sdsb5

file1.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<spaces>
    <space path="/dev/vg1000/lv" reference="/volume1" uuid="w7hmDd-j6T3-yWCV-9fEv-HE2Y-Vfrm-FHXoLi" device_type="1" drive_type="0" container_type="1" limited_raidgroup_num="12" >
        <device>
            <lvm path="/dev/vg1000" uuid="X3BAVZ-PqyQ-BNvJ-mYaf-9lAR-Up24-icJSsM" designed_pv_counts="2" status="normal" total_size="135898876346368" free_size="17151620546560" pe_size="4194304" expansible="0" max_size="132713751296">
                <raids>
                    <raid path="/dev/md3" uuid="11a49b00:599c89fe:09b6521c:2cea662f" level="raid6" version="1.2">
                        <disks>
                            <disk status="normal" dev_path="/dev/sdma6" model="WD60EFRX-68L0BN1        " serial="WD-WX21DA5K1801" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="0">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdmb6" model="WD60EFRX-68L0BN1        " serial="WD-WX31DB58YHUU" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="3">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdmc6" model="WD60EFRX-68L0BN1        " serial="WD-WX31D95HZLA2" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="5">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdna6" model="WD60EFRX-68L0BN1        " serial="WD-WX11DC580PX8" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="6">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdnb6" model="WD60EFRX-68L0BN1        " serial="WD-WXB1HB4UA48R" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="1">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdnc6" model="WD60EFRX-68L0BN1        " serial="WD-WX41DA57HNRZ" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="4">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdoa6" model="WD60EFRX-68L0BN1        " serial="WD-WX11DB65Z87L" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="8">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdob6" model="WD60EFRX-68L0BN1        " serial="WD-WX11DC6RDEYL" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="7">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdoc6" model="WD60EFRX-68L0BN1        " serial="WD-WX41DA58PT53" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="2">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdpa6" model="WD60EFRX-68L0BN1        " serial="WD-WX11DB5NET4F" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="10">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdpb6" model="WD60EFRX-68L0BN1        " serial="WD-WX11D37D9SN2" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="9">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdqb6" model="WD6002FFWX-68TZ4N0      " serial="K1JY4D5D" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="12">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdqc6" model="WD6002FFWX-68TZ4N0      " serial="K1JXXWRD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="11">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdra6" model="WD6002FFWX-68TZ4N0      " serial="K1K9W1LD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="17">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdrb6" model="WD6002FFWX-68TZ4N0      " serial="K1K9PZJD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="16">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdrc6" model="WD6002FFWX-68TZ4N0      " serial="K1KAEVDD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="15">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdsa6" model="WD6002FFWX-68TZ4N0      " serial="K1K9PYWD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="14">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdsb6" model="WD6002FFWX-68TZ4N0      " serial="K1KAGJVD" partition_version="8" partition_start="7814039104" partition_size="3906799104" slot="13">
                            </disk>
                        </disks>
                    </raid>
                    <raid path="/dev/md2" uuid="a11476dd:d11720f9:3ae54a77:a2fdb1eb" level="raid6" version="1.2">
                        <disks>
                            <disk status="normal" dev_path="/dev/sda5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130674014" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="0">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdb5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130688271" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="1">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdc5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130693259" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="2">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdd5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130619691" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="3">
                            </disk>
                            <disk status="normal" dev_path="/dev/sde5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130651575" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="4">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdf5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130681732" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="5">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdg5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130622745" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="6">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdh5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130726036" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="7">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdi5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130730013" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="8">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdj5" model="WD4000FYYZ-01UL1B0      " serial="WD-WCC130746174" partition_version="7" partition_start="9453280" partition_size="7804569728" slot="9">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdma5" model="WD60EFRX-68L0BN1        " serial="WD-WX21DA5K1801" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="12">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdmb5" model="WD60EFRX-68L0BN1        " serial="WD-WX31DB58YHUU" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="13">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdmc5" model="WD60EFRX-68L0BN1        " serial="WD-WX31D95HZLA2" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="15">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdna5" model="WD60EFRX-68L0BN1        " serial="WD-WX11DC580PX8" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="16">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdnb5" model="WD60EFRX-68L0BN1        " serial="WD-WXB1HB4UA48R" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="11">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdnc5" model="WD60EFRX-68L0BN1        " serial="WD-WX41DA57HNRZ" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="14">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdoa5" model="WD60EFRX-68L0BN1        " serial="WD-WX11DB65Z87L" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="18">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdob5" model="WD60EFRX-68L0BN1        " serial="WD-WX11DC6RDEYL" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="17">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdoc5" model="WD60EFRX-68L0BN1        " serial="WD-WX41DA58PT53" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="10">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdpa5" model="WD60EFRX-68L0BN1        " serial="WD-WX11DB5NET4F" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="20">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdpb5" model="WD60EFRX-68L0BN1        " serial="WD-WX11D37D9SN2" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="19">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdqb5" model="WD6002FFWX-68TZ4N0      " serial="K1JY4D5D" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="22">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdqc5" model="WD6002FFWX-68TZ4N0      " serial="K1JXXWRD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="21">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdra5" model="WD6002FFWX-68TZ4N0      " serial="K1K9W1LD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="27">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdrb5" model="WD6002FFWX-68TZ4N0      " serial="K1K9PZJD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="26">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdrc5" model="WD6002FFWX-68TZ4N0      " serial="K1KAEVDD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="25">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdsa5" model="WD6002FFWX-68TZ4N0      " serial="K1K9PYWD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="24">
                            </disk>
                            <disk status="normal" dev_path="/dev/sdsb5" model="WD6002FFWX-68TZ4N0      " serial="K1KAGJVD" partition_version="8" partition_start="9453280" partition_size="7804569728" slot="23">
                            </disk>
                        </disks>
                    </raid>
                </raids>
            </lvm>
        </device>
        <reference>
            <volumes>
                <volume path="/volume1" dev_path="/dev/vg1000/lv" uuid="w7hmDd-j6T3-yWCV-9fEv-HE2Y-Vfrm-FHXoLi" type="ext4">
                </volume>
            </volumes>
        </reference>
    </space>
</spaces>

文件2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<spaces>
<space path="/dev/md3" reference="/volume1" uuid="b9c08aae:78c4a659:658ead72:0b765dd7" device_type="2" drive_type="0" container_type="2" limited_raidgroup_num="12" >
    <device>
        <raid path="/dev/md3" uuid="b9c08aae:78c4a659:658ead72:0b765dd7" level="raid1" version="1.2">
            <disks>
                <disk status="normal" dev_path="/dev/sda3" model="WD1003FBYX-01Y7B0       " serial="WD-WCAW32967331" partition_version="8" partition_start="9437184" partition_size="1943883168" slot="0">
                </disk>
            </disks>
        </raid>
    </device>
    <reference>
        <volume path="/volume1" dev_path="/dev/md3" uuid="b9c08aae:78c4a659:658ead72:0b765dd7" type="ext4">
        </volume>
    </reference>
</space>
<space path="/dev/md2" reference="/volume2" uuid="f537fb36:6ab07e85:4ed56c52:02396e9e" device_type="2" drive_type="0" container_type="2" limited_raidgroup_num="12" >
    <device>
        <raid path="/dev/md2" uuid="f537fb36:6ab07e85:4ed56c52:02396e9e" level="raid1" version="1.2">
            <disks>
                <disk status="normal" dev_path="/dev/sdb3" model="ST2000DM001-9YN164      " serial="W1E15XDS" partition_version="8" partition_start="9437184" partition_size="3897387168" slot="0">
                </disk>
                <disk status="normal" dev_path="/dev/sdc3" model="ST2000DM001-1CH164      " serial="W1E4327N" partition_version="8" partition_start="9437184" partition_size="3897387168" slot="1">
                </disk>
            </disks>
        </raid>
    </device>
    <reference>
        <volume path="/volume2" dev_path="/dev/md2" uuid="f537fb36:6ab07e85:4ed56c52:02396e9e" type="ext4">
        </volume>
    </reference>
</space>
</spaces>

有没有办法在没有的情况下做到这一点xmlstarlet

答案1

尝试这个,

awk -F '"' '/dev_path/ {print $4} /raid path/ {print $2} /raid>/ {print $5}' file.xml | grep -v "vg" | tr '\n' ' '  | sed 's#  #\n#g'

/dev/md3 /dev/sdma6 /dev/sdmb6 /dev/sdmc6 /dev/sdna6 /dev/sdnb6 /dev/sdnc6 /dev/sdoa6 /dev/sdob6 /dev/sdoc6 /dev/sdpa6 /dev/sdpb6 /dev/sdqb6 /dev/sdqc6 /dev/sdra6 /dev/sdrb6 /dev/sdrc6 /dev/sdsa6 /dev/sdsb6
/dev/md2 /dev/sda5 /dev/sdb5 /dev/sdc5 /dev/sdd5 /dev/sde5 /dev/sdf5 /dev/sdg5 /dev/sdh5 /dev/sdi5 /dev/sdj5 /dev/sdma5 /dev/sdmb5 /dev/sdmc5 /dev/sdna5 /dev/sdnb5 /dev/sdnc5 /dev/sdoa5 /dev/sdob5 /dev/sdoc5 /dev/sdpa5 /dev/sdpb5 /dev/sdqb5 /dev/sdqc5 /dev/sdra5 /dev/sdrb5 /dev/sdrc5 /dev/sdsa5 /dev/sdsb5
  • 打印该行的第四个字段和第二个字段,分别以dev_path和作为分隔符(只是为了得到两行结果)raid path"/raid>/ {print $5}
  • 然后排除卷组grep -v
  • 然后将所有换行符转置为空格

  • 要获得两行输出,请将双空格替换为换行符。

给你第二个脚本

 awk -F '"' '/dev_path/ {print $4} /raid path/ {print $2} /reference>/ {print $5}' file.xml |grep -v "vg" | tr '\n' ' '  | sed 's#  #\n#g'

/dev/md3 /dev/sda3
/dev/md3
/dev/md2 /dev/sdb3 /dev/sdc3
/dev/md2

选项2:

 grep -wo  '/dev/.*"'  file.xml | grep -v vg |awk -F '"' '{print $1}' |  uniq | tr '\n' ' ' | sed -e 's#\/dev\/md#\n\/dev\/md#g'

相关内容