I am creating a bash script to setup tomcat server. I need to comment some contents from context.xml
file. I tried with sed but not able match the contents.
Here is full context.xml
file:
<?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>
I need to comment out the following line:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
Here is what I tried to replace the <Value......./>
with <!-- <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
The command runs without any error but it is not changing anything in original file. I guess the problem is because of new lines & extra space. I found solution for new line in How can I use sed to replace a multi-line string? and added \s*
to skip the space but it is still not working. I am not able to find any alternative for this.
Is there any easy way to achieve this? Or what is wrong with my command?
/^a test$/
, which is not in your input file. Please review that part and [edit] your question. – Quasímodo May 27 '20 at 23:07