태그 값을 추출하는 쉘 스크립트

태그 값을 추출하는 쉘 스크립트

아래에 설명된 XML 파일이 있고 unix 명령을 사용하여 애플리케이션 이름, 컴퓨터 및 상태 태그 값을 추출하고 이를 쉼표로 구분된 형식으로 표시하려고 한다고 가정합니다.

XML 파일:-

 <?xml version="1.0" encoding="UTF-8"?>
<applications>
<application name="Adapter/Code1">
<service name="Code1.par">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="Code1-One">
    <machine>123</machine>
    <status>Running</status>
</serviceInstance>
<serviceInstance name="Code1-Two">
    <machine>456</machine>
    <status>Running</status>
</serviceInstance>
</service>
</application>
<application name="Adapter/Code2">
<service name="Code2.par">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="Code2-One">
    <machine>123</machine>
    <status>Running</status>
</serviceInstance>
<serviceInstance name="Code2-Two">
    <machine>456</machine>
    <status>Running</status>
</serviceInstance>
</service>
</application>
</applications>

산출:-

Adapter/Code1,123,Running

Adapter/Code1,456,Running

Adapter/Code2,123,Running

Adapter/Code2,456,Running

이 활동을 수행하기 위한 unixcommand/shell 스크립트를 제공해 주실 수 있습니까?

미리 감사드립니다! ! !

답변1

파이썬3.x 솔루션(xml.etree.ElementTree기준 치수):

import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()
for app in root.findall('application'):
    for m,s in zip(app.iter('machine'), app.iter('status')):
        print("%s,%s,%s" % (app.get('name'), m.text, s.text))

산출:

Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

https://docs.python.org/3.6/library/xml.etree.elementtree.html?highlight=etree#module-xml.etree.ElementTree


xmlstarlet+( application각 요소의 하위 노드를 그룹화하기 위해) 해결 방법:

xmlstarlet sel -t -v "//application/@name| .//machine/text()| .//status/text()" -n input.xml 
 | awk '/Adapter/{app=$0; r=app; c=0; next}
   { if(++c==2){ c=0; print r","$0; r=app } else { r=r","$0 }}'

산출:

Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

  • "//application/@name| .//machine/text()| .//status/text()"- 필요한 노드를 얻기 위한 XPath 표현식

  • /Adapter/{app=$0; r=app; c=0; next}- application추가 연결을 위해 각 이름을 캡처합니다.

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html

답변2

설치하다히델그리고 xpath를 사용하세요.

내 생각에 가장 좋은 점은 다음과 같습니다 serviceInstance.

xidel f.xml -e '//serviceInstance/string-join((../../@name, machine, status),",")'
Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

답변3

xmlstarlet루프를 사용하여 각 serviceInstance노드를 반복합니다.

xmlstarlet sel -t \
    -m '//application/service/serviceInstance' \
    -v '../../@name' -o , \
    -v 'machine' -o , \
    -v 'status' -nl \
    file.xml

이는 serviceInstance노드와 일치하며, 각 노드에 대해 name상위 노드의 속성, machine노드 값 및 status노드 값을 추출합니다. 이러한 출력 사이에는 쉼표( )가 있고 끝에는 -o ,줄 바꿈( )이 있습니다.-nl

xq다음에서 다운로드할 수도 있습니다 .https://kislyuk.github.io/yq/):

xq -r '
    .applications.application[] | ."@name" as $name |
    .service.serviceInstance[]  | [ $name, .machine, .status ] | @csv' file.xml

답변4

XML 도구를 사용하지 않는 타당한 이유가 있는 경우 애플리케이션이 예제만큼 간단하다면 낮은 수준의 구문 분석을 사용할 수 있습니다.

sed 's/<application name="\([^"]*\)">/\1/
Ta
h
d
:a
/<machine>/!d
G
N
s_.*<machine>\(.*\)</machine>\n\(.*\)\n.*<status>\(.*\)</status>.*_\2,\1,\3_' yourfile.xml

관련 정보