2017-08-28 75 views
0

我有一個包含以下內容的XML文件通過XML標籤迭代 -使用bash

<File Name="something.txt" > 
<EmailWhenMissing>Customer</EmailWhenMissing> 
<CustomerEmail>[email protected]</CustomerEmail> 
</File> 
<File Name="somethingElse.txt" > 
<EmailWhenMissing>Customer</EmailWhenMissing> 
<CustomerEmail>[email protected]</CustomerEmail> 
</File> 

有什麼辦法,或通過各<File>元素循環最好的方式,這樣我可以對他們分別執行命令? (即,這樣我就能夠識別哪個文件標籤有空白或缺失的元素)

+0

我建議您使用XML模塊在perl或python中編寫腳本。 –

回答

1

如果我正確理解你的目標是驗證存在於每個File領域,考慮以下爲相關的例子:

#!/bin/bash 
#  ^^^^- IMPORTANT: not /bin/sh 

sep=$'\v' # pick a character that can't be in your data 

while IFS="$sep" read -r Name EmailWhenMissing CustomerEmail; do 
    # the line below this provides verbose logging when running with bash -x 
    : Name="$Name" EmailWhenMissing="$EmailWhenMissing" CustomerEmail="$CustomerEmail" 
    [[ $EmailWhenMissing ]] || { echo "File $Name is missing EmailWhenMissing"; } 
    [[ $CustomerEmail ]] || { echo "File $Name is missing CustomerEmail"; } 
done < <(xmlstarlet sel -t -m '//File' \ 
      -v ./@Name -o "$sep" \ 
      -v ./EmailWhenMissing -o "$sep" \ 
      -v ./CustomerEmail -n) 

鑑於以下輸入文件:

<root> 
    <File Name="something.txt"> 
    <EmailWhenMissing>Customer</EmailWhenMissing> 
    <CustomerEmail>[email protected]</CustomerEmail> 
    </File> 
    <File Name="somethingElse.txt"> 
    <EmailWhenMissing>Customer</EmailWhenMissing> 
    <CustomerEmail>[email protected]</CustomerEmail> 
    </File> 
    <File Name="NoEmailWhenMissing.txt"> 
    <CustomerEmail>[email protected]</CustomerEmail> 
    </File> 
    <File Name="NoCustomerEmail.txt"> 
    <EmailWhenMissing>Customer</EmailWhenMissing> 
    </File> 
    <File Name="EmptyFile.txt"/> 
</root> 

...它的輸出是:

File NoEmailWhenMissing.txt is missing EmailWhenMissing 
File NoCustomerEmail.txt is missing CustomerEmail 
File EmptyFile.txt is missing EmailWhenMissing 
File EmptyFile.txt is missing CustomerEmail 

隨着這裏對於bash的代碼,一些有用的閱讀:

  • BashFAQ #1 - 我怎樣才能讀取一個文件(數據流,可變)逐行(和/或逐場?)
  • BashFAQ #24 - 我在變量中設置了一個循環管道。爲什麼它們在循環終止後消失?或者,爲什麼我不能讀取數據? - 解釋了< <(...)循環表單的推理。
1

您可以使用xmlstarlet

for f in $(xmlstarlet select -t -v '//File/@Name' file.xml) 
do 
    echo $f 
done 
+1

用於XMLStarlet的+1,用於提示'for'循環遍歷面向行的輸出。請參閱http://mywiki.wooledge.org/DontReadLinesWithFor –