2013-08-10 2 views
1

XML 파일의 디렉토리가 있습니다. 각 파일에는 고유 한 식별자가 있습니다. 각 파일에는 고유 한 ID가있는 다른 파일 (별도의 디렉토리에 있음)에 대한 하나 이상의 참조가 들어 있습니다.bash에서 XPath를 사용하여 여러 파일 출력

예를 들어, 나는 example01.xml라는 이름의 파일이 : 파일이 다수에게 relatedFiles/otherFile 요소를 가지고 있다면,이 값을 concatinating, 각 @href의 파일의 복사본을 생성하고 이름을 바꿀 필요가

<file> 
    <fileId>xyz123</fileId> 
    <fileContents>Blah blah Blah</fileContents> 
    <relatedFiles> 
     <otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&amp;id=123‌​4'> 
      <title>Some resource</title> 
     </otherFile> 
     <otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&amp;id=4321'> 
      <title>Some other resource</title> 
     </otherFile> 
    </relatedFiles> 
</file> 

고유 ID는 @href이고 값은 fileID입니다. 예를 들어 파일 example01.xml의 복사본 2 개를 만들고 하나는 abc01_xyz123.xml, 다른 하나는 abc0002_xyz123.xml으로 만들어야합니다. 이것은 otherFile 요소만큼 많은 복사본을 만들도록 확장되어야합니다.

지금은 otherFile 요소가 하나만있는 경우이 작업을 수행하는 bash 스크립트가 있지만 스크립팅 기술이 제한되어 있고 여러 개의 otherFile 요소를 처리하는 방법을 파악하는 데 문제가 있습니다.

#!/bin/bash 
for f in *.xml; 
    do 
     name=`xpath -e 'string(//otherFile/@href)' $f 2> /dev/null` 
     echo "Moving" $f "to" ${name:3}.xml 
     echo $name 
     mv $f ${name:3}.xml 
    done 

미리 감사드립니다.

답변

1

이런 식으로 뭔가를 작동 할 수 있습니다 :

#!/bin/bash 

for f in *.xml; do 
    fid=$(xpath -e '//fileId/text()' "$f" 2>/dev/null) 
    for uid in $(xpath -e '//otherFile/@href' "$f" 2>/dev/null | awk -F= '{gsub(/"/,"",$0); print $3}'); do 
    echo "Moving $f to ${fid}_${uid}.xml" 
    cp "$f" "${fid}_${uid}.xml" 
    done 
    rm "$f" 
done 
+0

이 예제 파일과 함께 완벽하게 작동하지만 실제 값'@ href'이 같은 외모와 함께 일하고 있어요 속성 :'에 http : // 서브 .domain.abc.edu/directory/index.php? p = collections/pageview & id = 1234'. 스크립트에서 속성 값을 파싱하는 방법은 무엇입니까? – tat

+0

알았습니다! 'awk' 스크립트에서 필드 값을 $ 4로 변경해야했습니다. 도와 줘서 고마워. – tat

관련 문제