web-dev-qa-db-ja.com

sedを使用してxmlファイルの内容をコメント化する方法は?

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
Afsar edrisy

これを試して:

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>
2
seshoumara

試してください:

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
3
fpmurphy

transform.xslのXSL変換でxsltprocまたはxmlstarletを使用する:

<?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">&lt;!-- </xsl:text>
    <xsl:copy-of select="."/>
    <xsl:text disable-output-escaping="yes"> --&gt;</xsl:text>
  </xsl:template>

</xsl:transform>

最初のテンプレートは単なるアイデンティティ変換ですが、2番目のテンプレートは<!--ノードの周囲に-->/Context/Valveを挿入します。

ノードの属性の正確な値を一致させる必要がある場合は、2番目のテンプレートの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
1
Kusalananda