2017-10-23 9 views
1

아래의 (잘린) XML 파일에서 프로그램 제목과 부제목을 추출하려고합니다. 나는 xmllint와 sed를 사용하여 개별적으로 압축을 풀고 하나의 파일로 결합했지만, 제목과 하위 제목이없는 가끔 항목이 있다는 것을 발견했습니다. 이 경우 하위 제목을 비워 두는 것이 좋습니다. 누군가이 불일치를 설명 할 수있는 방법을 제안 할 수 있습니까?xml 태그를 캡쳐 한 후 관련 태그가있는 경우 다음을 입력하십시오.

XML 파일

<programme start="20171013170000 +0100" stop="20171013180000 +0100" channel="b492458d826d592ec7c528545a16c757"> 
    <title lang="eng">Accessories Gift Hall</title> 
    <sub-title lang="eng">Find the perfect gift with fashion accessories by some of our most sought-after brands. From chic purses and wallets to cosy PJs and slippers, there&apos;s something for everyone.</sub-title> 
</programme> 
<programme start="20171013180000 +0100" stop="20171014130000 +0100" channel="b492458d826d592ec7c528545a16c757"> 
    <title lang="eng">..programmes start again at 1pm</title> 
</programme> 
<programme start="20171014130000 +0100" stop="20171014140000 +0100" channel="b492458d826d592ec7c528545a16c757"> 
    <title lang="eng">Ruth Langsford&apos;s Fashion Edit</title> 
    <sub-title lang="eng">TV personality and QVC fashion ambassador, Ruth Langsford, shares her favourite looks and must-have pieces that will transform your wardrobe and have you looking fabulously stylish.</sub-title> 
</programme> 

배쉬는 V1에게

xmllint --xpath "//programme/title" xmltv | sed -r 's/\n//g' | sed 's/<\/title>/\n/g' | sed 's/<title lang="eng">//g' > 1.txt 
xmllint --xpath "//programme/sub-title" xmltv | sed -r 's/\n//g' | sed 's/<\/sub-title>/\n/g' | sed 's/<sub-title lang="eng">//g' > 2.txt 
paste <(cat 1.txt) <(cat 2.txt) > 3.txt 

감사를 명령!

+0

xmlstarlet이이 작업에 더 적합한 선택이라고 생각합니다. xmlstarlet 솔루션에 관심이 있으십니까? –

+1

네, Daniel, xmlstarlet에 대해서도 들었습니다. xmlstarlet과 xmllint의 차이점이 무엇인지 잘 모릅니다. – user2679016

+0

차이점은 xmlstarlet은 개행 문자로 각 결과 행을 표시한다는 것입니다 (xmllint의 경우는 해당되지 않음) –

답변

1

한 패스 다음 명령 줄에서 xmlstarletsel 명령을 사용하여 예입니다 ...에서

$ xmlstarlet sel -T -t -m '//programme' -v 'concat(normalize-space(title)," ",normalize-space(sub-title))' -n input.xml 
Accessories Gift Hall Find the perfect gift with fashion accessories by some of our most sought-after brands. From chic purses and wallets to cosy PJs and slippers, there's something for everyone. 
..programmes start again at 1pm 
Ruth Langsford's Fashion Edit TV personality and QVC fashion ambassador, Ruth Langsford, shares her favourite looks and must-have pieces that will transform your wardrobe and have you looking fabulously stylish. 

제목과 하위 제목을 하나의 공백으로 구분하지만 그 내용은 차일 수 있습니다. nged.

+1

그 모양이 정확하고 빠릅니다! 대신 xmlstarlet을 배워야한다고 생각합니다! 감사! – user2679016

+0

좋은 샷, 예. –

0

내가 어떻게 할 것인지 :

#!/bin/bash 

count=$(xmllint --xpath "count(//programme)" /tmp/file.xml) 

for ((i=1; i<=count; i++)); do 
    xmllint --xpath "//programme[$i]/title/text()" /tmp/file.xml 
    echo -n '|' 
    xmllint --xpath "//programme[$i]/sub-title/text()" /tmp/file.xml 
    echo 
done 
+0

좋지만 느립니다. 지금까지 전체 파일에서 15 분, 몇 가지 "XPath 세트가 비어 있습니다"오류가 발생했습니다. – user2679016

0

나오지

sed '/<title/!d;N;/<sub-title/!s/\n.*//' XML File 
+0

누군가가 XML을 포맷 할 때까지 'title'또는 'subtitle'이 모두 같은 줄에 있지 않습니다. (https://stackoverflow.com/a/1732454/317052) –

+0

예 XML을 제거하고 타이틀과 하위 제목이 서로 이상적으로 있어야합니다. – user2679016