![검색 텍스트가 있는 xml 노드를 가져옵니다.](https://linux55.com/image/126064/%EA%B2%80%EC%83%89%20%ED%85%8D%EC%8A%A4%ED%8A%B8%EA%B0%80%20%EC%9E%88%EB%8A%94%20xml%20%EB%85%B8%EB%93%9C%EB%A5%BC%20%EA%B0%80%EC%A0%B8%EC%98%B5%EB%8B%88%EB%8B%A4..png)
아래의 것과 비슷한 형식이 잘 지정되고 들여쓰기된 XML이 있습니다. 텍스트가 포함된 모노 라벨을 가져와야 합니다.http://yahoo.com"
<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>
다음 명령을 사용하려고합니다.
cat -n index.xml | sed -n "/root/,/root>/p" |grepp"http://yahoo.com"
6 <tag1>http://yahoo.com</tag1>
12 <tag1>http://yahoo.com</tag1>
이런 출력이 필요합니다. 하지만 내 검색 텍스트가 포함된 단일 노드를 얻는 방법을 모르겠습니다.
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
답변1
사용XML 스타:
$ xmlstarlet sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono><mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
XPath는 " 값이 호출되는 하위 노드가 있는 //mono[tag1 = "http://yahoo.com"]
모든 노드"를 의미합니다 . mongo
"...의 복사본을 주세요"를 의미하고 마지막으로 출력 끝에 개행 문자를 삽입합니다.tag1
http://yahoo.com
-c
-nl
id
노드의 다음과 일치시키려고 합니다 .
xmlstarlet sel -t -v '//mono[tag1 = "http://yahoo.com"]/@id' -nl file.xml