sed 명령을 사용하여 xml 파일의 특정 위치에 텍스트 삽입

sed 명령을 사용하여 xml 파일의 특정 위치에 텍스트 삽입

sed 명령을 사용하여 xml 파일에 텍스트를 삽입하는 방법을 알아내려고 합니다. 나는 몇 가지 기본 명령을 시도하면서 sed를 사용하기 시작했습니다.

sed -i -e ' <property name="prop1" ref="ANOTHER_BEAN" /> ' my_config_file.xml  

이 줄을 추가하지만 내가 원하는 곳에는 추가되지 않습니다.

내 특정 사례의 문제는 텍스트를 어딘가에 추가해야 한다는 것입니다(Java 구성 파일이므로 일부 빈).

 <bean id="BEAN_ID_1"
    class="com.toto.BeanClass1"
    scope="prototype">
    <property name="prop1" ref="ANOTHER_BEAN_1" />
    <property name="prop2" ref="BEAN1" />
 </bean>

<bean id="BEAN_ID_2"
    class="com.toto.BeanClass2"
    scope="prototype">
    <property name="prop_1" ref="ANOTHER_BEAN_2" />
    <property name="prop_2" ref="BEAN2" />
 </bean>

이 속성을 둘러싸는 태그 앞에 "BEAN_ID_1"이라는 이름의 Bean에 추가하고 싶습니다.

 <property name="property" ref="ANOTHER_BEANXXX" />

따라서 출력은 다음과 같습니다.

 <bean id="BEAN_ID_1"
    class="com.toto.BeanClass1"
    scope="prototype">
    <property name="prop1" ref="ANOTHER_BEAN_1" />
    <property name="prop2" ref="BEAN1" />
    <property name="property" ref="ANOTHER_BEANXXX" />
 </bean>

<bean id="BEAN_ID_2"
    class="com.toto.BeanClass2"
    scope="prototype">
    <property name="prop_1" ref="ANOTHER_BEAN_2" />
    <property name="prop_2" ref="BEAN2" />
 </bean> 

추신: 프로덕션에서는 파일이 어떻게 생겼는지 모르기 때문에 줄 번호에 의존할 수 없습니다.

누구든지 이 문제를 해결하도록 도와줄 수 있나요? 감사합니다

답변1

XML 파서/편집기를 사용하여 XML을 편집하는 것이 sed.

귀하의 XML 예제를 수정하고 모두 마무리하여 <beans>...</beans>유효한 XML로 만들었습니다. 다음을 사용하는 솔루션은 다음과 같습니다 xmlstarlet.

xmlstarlet edit --subnode '//bean[@id="BEAN_ID_1"]' -t elem -n property --var this '$prev' --insert '$this' -t attr -n name -v property --insert '$this' -t attr -n ref -v 'ANOTHER_BEANXXX' configFile

산출

<?xml version="1.0"?>
<beans>
  <bean id="BEAN_ID_1" class="com.toto.BeanClass1" scope="prototype">
    <property name="prop1" ref="ANOTHER_BEAN_1"/>
    <property name="prop2" ref="BEAN1"/>
    <property name="property" ref="ANOTHER_BEANXXX"/>
  </bean>
  <bean id="BEAN_ID_2" class="com.toto.BeanClass2" scope="prototype">
    <property name="prop_1" ref="ANOTHER_BEAN_2"/>
    <property name="prop_2" ref="BEAN2"/>
  </bean>
</beans>

이 줄에 대한 설명 xmlstarlet:

# Edit the XML, writing the result to stdout
xmlstarlet edit

# Create a subnode called "property" of "//bean" having an attribute id="BEAN_ID_1"
--subnode '//bean[@id="BEAN_ID_1"]' -t elem -n property

# Identify this new element as "this"
--var this '$prev'

# Insert an attribute called "name" with a value "property" into the new element
--insert '$this' -t attr -n name -v property

# Insert an attributed called "ref" with a value "ANOTHER_BEANXXX" into the new element
--insert '$this' -t attr -n ref -v 'ANOTHER_BEANXXX'

# XML source file
configFile

답변2

xml 파서를 사용하여 xml을 구문 분석하는 것이 더 강력할 수 있습니다.

하지만 아마도 그럴까요?

sed -i '/<bean id="BEAN_ID_1"/,/<\/bean>/ s/<\/bean>/   <property name="property" ref="ANOTHER_BEANXXX" \/>\n<\/bean>/' my_config_file.xml

<bean id="BEAN_ID_1"이는 시작하고 끝나는 범위에서 </bean>앞에 행을 삽입한다는 의미입니다.<property name="property" ref="ANOTHER_BEANXXX" /></bean>

관련 정보