2014-01-16 2 views
0

의 값을 변경 SED를 사용 :XML 파일 -이 xml 파일이 속성

<?xml version="1.0" encoding="UTF-8"?> 
<testsuites> 
    <testsuite errors="0" failures="0" hostname="emulator-5554_tel_unknown_sdk" name="com.example.android.apis.tests.robotium" tests="3" time="4.4730" timestamp="Thu Jan 16 14:34:09 CET 2014"> 
     <properties> 
     <property name="PROCESSOR_ARCHITECTURE" value="AMD64" /> 
     <property name="file.encoding.pkg" value="sun.io" /> 
     </properties> 
    <testcase classname="android.test.AndroidTestCase" name="testAndroidTestCaseSetupProperly" time="0.1750" /> 
    </testsuite> 
</testsuites> 

나는 SED 명령으로 초보자입니다. testsuite 요소의 name 속성을 수정하고 싶습니다. 그 값에 호스트 이름 속성의 값을 접두어로 붙이고 싶습니다.

sed로 이런 종류의 생각을 할 수 있습니까? 코드 스 니펫이 있습니까?

고마워요. a.awk이 어디

gawk -f a.awk RS="^$" file.xml 

:은 GNU awk는 버전 4를 사용

엠마누엘

+1

그것은 나쁜 'xml'을 파싱하는 아이디어 'sed'. 너는 그걸로 붙어 있니? – Birei

+0

나쁜 아이디어 인 경우, 가장 좋은 아이디어는 무엇입니까? – Gillespie59

+1

'xslt' 또는'java','python','perl','ruby'와 수백 개의 좋은 파서를 가진 언어가 있습니다. – Birei

답변

1

, 당신은 뭔가를 시도 할 수

{ 
    n=split($0,a,/<testsuite [^>]*>/,s) 
    printf "%s%s",s[0],a[1] 
    for (i=1; i<n; i++) { 
     m=split(s[i],b,/(name|hostname)="[^"]*"/,t) 
     printf "%s%s",t[0],b[1] 
     q=length(t[1]) 
     z=substr(t[1],11,q-11) 
     q=length(t[2]) 
     w=substr(t[2],7,q-7) 
     t[2]="name=\""z ":" w "\"" 
     printf "%s%s%s%s%s%s",t[1],b[2],t[2],b[3],t[3],a[i+1] 
    } 
    printf "%s", s[n] 
} 

출력은 :

<?xml version="1.0" encoding="UTF-8"?> 
<testsuites> 
    <testsuite errors="0" failures="0" hostname="emulator-5554_tel_unknown_sdk" name="emulator-5554_tel_unknown_sdk:com.example.android.apis.tests.robotium" tests="3" time="4.4730" timestamp="Thu Jan 16 14:34:09 CET 2014"> 
     <properties> 
     <property name="PROCESSOR_ARCHITECTURE" value="AMD64" /> 
     <property name="file.encoding.pkg" value="sun.io" /> 
     </properties> 
    <testcase classname="android.test.AndroidTestCase" name="testAndroidTestCaseSetupProperly" time="0.1750" /> 
    </testsuite> 
</testsuites>