Tomcat 서버를 설정하기 위해 bash 스크립트를 만들고 있습니다. context.xml
파일에 뭔가 주석을 달아야 합니다 . sed를 사용해 보았으나 내용이 일치하지 않았습니다.
전체 context.xml
파일은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
다음 줄을 주석 처리해야 합니다.
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
이것이 내가 다음으로 <Value......./>
대체하려고 시도한 것입니다.<!-- <Value........./> -->
sed '/^a test$/{$!{N;s/^<Valve className="org.apache.catalina.valves.RemoteAddrValve"\n\s.*allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" \/>/<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"\n allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" \/>-->/;ty;P;D;:y}}' context.xml
명령은 오류 없이 실행되었지만 원본 파일에서는 아무 것도 변경되지 않았습니다. 문제는 새로운 줄과 추가 공간 때문이라고 생각합니다. 새로운 라인에 대한 해결책을 찾았습니다sed를 사용하여 여러 줄 문자열을 바꾸는 방법은 무엇입니까?건너뛰는 공백을 추가했지만 \s*
여전히 작동하지 않습니다. 대안을 찾을 수 없습니다.
이것을 달성하는 쉬운 방법이 있습니까? 아니면 내 명령에 문제가 있는 걸까요?
답변1
노력하다:
sed '{$!{N;s/<Valve.*\n.*allow.* \/>/<!-- & -->/;ty;P;D;:y}}' content.xml
다음과 같이 출력됩니다.
...
<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
필요한 경우 주석 레이블을 별도의 줄에 배치하도록 쉽게 수정할 수 있습니다.
sed '{$!{N;s/<Valve.*\n.*allow.* \/>/<!--\n&\n-->/;ty;P;D;:y}}' content.xml
답변2
이 시도:
sed -r '/^\s*<Valve className\s*=\s*"org\.apache\.catalina\.valves\.RemoteAddrValve"\s*$/{h;z;N;s:^\n::;H;/^\s*allow\s*=\s*"127\\\.\\d\+\\\.\\d\+\\\.\\d\+\|::1\|0:0:0:0:0:0:0:1"\s*\/>\s*$/{g;s/.*/<!--\n&\n-->/}}' context.xml
산출:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
답변3
XSL 변환과 함께 xsltproc
또는 를 사용하십시오 .xmlstarlet
transform.xsl
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/|node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Context/Valve">
<xsl:text disable-output-escaping="yes"><!-- </xsl:text>
<xsl:copy-of select="."/>
<xsl:text disable-output-escaping="yes"> --></xsl:text>
</xsl:template>
</xsl:transform>
첫 번째 템플릿은 단지 항등 변환인 반면, 두 번째 템플릿은 노드 주위에 합계를 삽입합니다 <!--
.-->
/Context/Valve
노드에 있는 속성의 정확한 값을 일치시켜야 하는 경우 match
두 번째 템플릿에서 보다 구체적인 XPath 쿼리를 사용하여 이를 수행합니다.
<xsl:template match="/Context/Valve[
@className='org.apache.catalina.valves.RemoteAddrValve' and
@allow='127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1'
]">
xmlstarlet tr transform.xsl file.xml
적용은 다음을 사용하여 수행할 수 있습니다 xsltproc transform.xsl file.xml
.
$ xsltproc transform.xsl file.xml
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true">
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"/> -->
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
이는 형식에 관계없이 노드 주석 처리를 처리합니다.
변경되지 않는 정적 파일인 경우 올바른 줄에 주석 태그를 삽입하세요.
sed '17s/^/<!-- /; 18s/$/ -->/' file.xml