Bash:从复杂的XML文件中检索字段

时间:2017-04-13 16:20:25

标签: xml bash shell

我尝试使用shell命令(例如sed,egrep,awk)从具有复杂结构的XML文件中检索某些字段,但它失败了。

这是我的文件file.xml的结构(它用不同的值重复多次):

<Task ID="186155128" Name="Partner: test" Active="1" NextEID="14" AR="0">
        <Schedules>
            <Schedule OnlyUntilFirstSuccess="0" FailIfNoSuccessInSched="0" RunEvenIfNotif="0">
                <Days>
                    <DayOfWeek>Monday</DayOfWeek>
                    <DayOfWeek>Tuesday</DayOfWeek>
                    <DayOfWeek>Wednesday</DayOfWeek>
                    <DayOfWeek>Thursday</DayOfWeek>
                    <DayOfWeek>Friday</DayOfWeek>
                    <DayOfWeek>Saturday</DayOfWeek>
                    <DayOfWeek>Sunday</DayOfWeek>
                </Days>
                <Frequency>
                    <Interval StartTime="00:00" EndTime="23:59" EveryMinutes="15"/>
                </Frequency>
            </Schedule>
        </Schedules>
        <Destination HostID="1027635962" Type="AS2" UseDefPartner="1" UseDefRetryCount="1" UseDefRetryTimeoutSecs="1" Filename="[OrigName]" RequestMDN="1" RequestSigned="0" />
        <Source HostID="165429109" Type="siLock" FolderID="" FolderType="-1" FolderName="home/test" FileMask="*.*" DeleteOrig="1" NewFilesOnly="0" SearchSubdirs="0" Unzip="0" />

</Task>

<MyID="1027635962" Host1="myhost" DefPartnerURL="https://site.site.com" Password="!s!f7xtVc=="/>
<FID="165429109" DotNET="1" Name="mytest" Host2="testy.com" Port="443" />

我想获得输出:Name; HostID; FolderName; Host1; Host2

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果我们假设您的意思而不是(无效)字符串<MyID="1027635962" Host1="myhost"/>而不是(有效)字符串<MyID id="1027635962" Host1="myhost"/>,当然也假设这两个基准都是有效文档的一部分,这可能看起来像:

xmlstarlet sel -t -m '//Task' \
  -v ./@Name -o ';' \
  -v ./Source/@HostID -o ';' \
  -v ./Destination/@Filename -o ';' \
  -v '//MyID[@id=./Destination/HostID]/@Host1' -o ';' \
  -v '//FID[@id=./Source/HostID]/@Host2' -n \
  <in.xml >out.txt