2012-11-15 4 views
0

도와주세요! ... 내 resoultion 검색 시간을 보내고 있고, 나는 내 머리로 벽을 타격하고 내가 나오지도 사용하고 싶은 모든입니다 포함 찾기 태그는 "번호는 삭제 된"문자열 및xml 태그를 제외한 sed 특정 사례

제거

입력 :

<Cell ss:StyleID="s128"/> 
    <Cell ss:StyleID="s128"/> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell ss:StyleID="s81"><Data ss:Type="String">Number Deleted</Data></Cell> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s82"><Data ss:Type="Boolean">0</Data></Cell> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s82"><Data ss:Type="Boolean">0</Data></Cell> 
    <Cell ss:StyleID="s83"><Data ss:Type="String">-1</Data></Cell> 
    <Cell ss:StyleID="s81"><Data ss:Type="String">&quot;Deleted:&quot;</Data></Cell> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s81"/> 
    <Cell ss:StyleID="s81"/> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="String">Number Saved</Data></Cell> 
    <Cell ss:Index="5"><Data ss:Type="Boolean">0</Data></Cell> 
    <Cell ss:Index="7"><Data ss:Type="Boolean">0</Data></Cell> 

출력 :

<Cell ss:StyleID="s128"/> 
    <Cell ss:StyleID="s128"/> 
    </Row> 

    <Row ss:AutoFitHeight="0"> 
    <Cell><Data ss:Type="String">Number Saved</Data></Cell> 
    <Cell ss:Index="5"><Data ss:Type="Boolean">0</Data></Cell> 
    <Cell ss:Index="7"><Data ss:Type="Boolean">0</Data></Cell> 

지금까지 내가 알아 낸, 태그의 마지막까지 "번호 삭제"에서 선을 exluding XML을 볼 수 있지만이 XML의 무결성에 대한 잘못된 수행하는 방법 , 태그가 닫히지 않았 으면 여기 내가 가지고있는 것입니다 :

function filter_xml 
{ 
    START="<Cell ss:StyleID="s81"><Data ss:Type="String">Number Deleted" 
    END="<\/Row>" 
    sed "/$START/,/$END/d" file.xml 
} 
+1

. 복잡한 중첩에서는 sed 또는 grep 사용이 실패합니다. –

+0

확실한 것은, 그 sed는 적합하지 않지만, 나를 죽여라 :) 그것은 sed되어야한다. – pawel3ala

답변

1

XML 인식 도구를 사용하십시오. 예를 들어, xsh를 들어 :

open file.xml ; 
remove //Row[Cell/Data/text()='Number Deleted'] ; 
save :b ; 
1

나는 sed는 XML 파일 처리를위한 최고의 도구입니다 생각하지 않습니다.

실제로 XML 파일을 구문 분석 할 수 없습니까? 여기

python와 일부 신속하고 더러운 예는 다음과 같습니다

에서/tmp를/데이터 파일 :

<data xmlns:ss="foobar"> 
<Row> 
<Cell ss:StyleID="s128"/> 
<Cell ss:StyleID="s128"/> 
</Row> 
<Row ss:AutoFitHeight="0"> 
<Cell ss:StyleID="s81"><Data ss:Type="String">Number Deleted</Data></Cell> 
<Cell ss:StyleID="s83"><Data ss:Type="String">-1</Data></Cell> 
</Row> 
<Row ss:AutoFitHeight="0"> 
<Cell><Data ss:Type="String">Number Saved</Data></Cell> 
<Cell ss:Index="5"><Data ss:Type="Boolean">0</Data></Cell> 
</Row> 
</data> 

파이썬 코드 :

import xml.dom.minidom as Xml 
file = "/tmp/data" 
xmlDoc = Xml.parse(file) 
for row in xmlDoc.getElementsByTagName("Row"): 
    if "Number Deleted" not in row.toprettyxml(): 
    print row.toxml() 

출력 :

<Row> 
<Cell ss:StyleID="s128"/> 
<Cell ss:StyleID="s128"/> 
</Row> 
<Row ss:AutoFitHeight="0"> 
<Cell><Data ss:Type="String">Number Saved</Data></Cell> 
<Cell ss:Index="5"><Data ss:Type="Boolean">0</Data></Cell> 
</Row> 
0

당신 (GNU이 나오지도)에 대한이 작동하지 않을 수 있습니다 : 당신은 작동이 유형의 XML 도구를 사용한다

sed '/<Row /!b;:a;$bb;N;/.*\n[^\n]*<\/Row>/!ba;:b;/Number Deleted/d' file 
관련 문제