
Source file example:
<HDR></HDR><b></b><c></c>
(한 줄로 생성된 XML 파일)
또는
Source file example:
<HDR>
</HDR>
<b>
</b>
<c>
</c>
<b>
두 소스 형식 모두에서 이전 파일의 모든 내용을 삭제해야 합니다 . 나는 다음 방법을 사용해 보았습니다.
sed 's/^.*b/b/'
그러나 그것이 그것을 대체하지는 않습니다. 다른 방법이 있으면 알려주세요.
답변1
XML 문서가 다음과 같이 잘 구성되어 있다고 가정합니다.
<document>
<HDR>
</HDR>
<b>
</b>
<c>
</c>
</document>
그럼 당신은 사용할 수 있습니다XML 스타HDR
아래와 같이 모든 태그를 제거합니다 .
xmlstarlet ed -d '//HDR' file.xml >newfile.xml
태그 HDR
바로 뒤에 있는 태그 만 제거합니다 .b
xmlstarlet ed -d '//HDR[following-sibling::*[1][name() = "b"]]' file.xml >newfile.xml
XMLStarlet을 사용하여 태그 내용을 수정할 수도 있습니다.
$ xmlstarlet ed -u '//HDR[following-sibling::*[1][name() = "b"]]' -v 'New header value' file.xml
<?xml version="1.0"?>
<document>
<HDR>New header value</HDR>
<b/>
<c/>
</document>
$ xmlstarlet ed -i '//HDR[following-sibling::*[1][name() = "b"]]' -t attr -n 'new_attribute' -v 'hello' file.xml
<?xml version="1.0"?>
<document>
<HDR new_attribute="hello"/>
<b/>
<c/>
</document>
답변2
질문:
이전 파일의 모든 내용 삭제
<b>
답변:
perl -0777 -lape 's/^.*<b>/<b>/s'
테스트 실행:
==> in1.txt <==
<HDR></HDR><b></b><c></c>
==> in2.txt <==
<HDR>
</HDR>
<b>
</b>
<c>
</c>
$ perl -i -0777 -lape 's/^.*<b>/<b>/s' in{1,2}.txt
==> in1.txt <==
<b></b><c></c>
==> in2.txt <==
<b>
</b>
<c>
</c>
답변3
유형 1:
echo "<HDR></HDR><b></b><c></c>" | sed 's/^.*<b>/<b>/'
<b></b><c></c>
<b>
모든 것을 대체할 것이다<b>
유형 2:
sed -n '/<b>/,$p' file
<b>
</b>
<c>
</c>
<b>
파일의 끝 부분($)에 첫 번째 항목을 인쇄합니다 .