Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

shell - Extract xml tag value using awk command

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...