순수 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'

관련 정보