Extract xml tag value using awk command

南笙酒味 提交于 2019-12-03 17:59:44

问题


I have a xml like below

<root>    
<FIToFICstmrDrctDbt>
            <GrpHdr>
                <MsgId>A</MsgId>
                <CreDtTm>2001-12-17T09:30:47</CreDtTm>
                <NbOfTxs>0</NbOfTxs>
                <TtlIntrBkSttlmAmt Ccy="EUR">0.0</TtlIntrBkSttlmAmt>
                <IntrBkSttlmDt>1967-08-13</IntrBkSttlmDt>
                <SttlmInf>
                    <SttlmMtd>CLRG</SttlmMtd>
                    <ClrSys>
                        <Prtry>xx</Prtry>
                    </ClrSys>
                </SttlmInf>
                <InstgAgt>
                    <FinInstnId>
                        <BIC>AAAAAAAAAAA</BIC>
                    </FinInstnId>
                </InstgAgt>
            </GrpHdr>
    </FIToFICstmrDrctDbt>
</root>

I need to extract the value of each tag value in separate variables using awk command. how to do it?


回答1:


You can use awk as shown below, however, this is NOT a robust solution and will fail if the xml is not formatted correctly e.g. if there are multiple elements on the same line.

$ dt=$(awk -F '[<>]' '/IntrBkSttlmDt/{print $3}' file)
$ echo $dt
1967-08-13

I suggest you use a proper xml processing tool, like xmllint.

$ dt=$(xmllint --shell file <<< "cat //IntrBkSttlmDt/text()" | grep -v "^/ >")
$ echo $dt
1967-08-13



回答2:


The following gawk command uses a record separator regex pattern to match the XML tags. Anything starting with a < followed by at least one non-> and terminated by a > is considered to be a tag. Gawk assigns each RS match into the RT variable. Anything between the tags will be parsed as the record text which gawk assigns to $0.

gawk 'BEGIN { RS="<[^>]+>" } { print RT, $0 }' myfile



回答3:


below code stores all the tag values in an array!hope this helps. But i still belive this is not an optimal way to do it.

> perl -lne 'if(/>[^<]*</){$_=~m/>([^<]*)</;push(@a,$1)}if(eof){foreach(@a){print $_}}' temp
A
2001-12-17T09:30:47
0
0.0
1967-08-13
CLRG
xx
AAAAAAAAAAA


来源:https://stackoverflow.com/questions/14054203/extract-xml-tag-value-using-awk-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!