Stella H.: tomcat-users.xml Datei bearbeiten

Hallo!

ich habe folgende XML-Datei:

<tomcat-users>  
<!--  
  <role rolename="tomcat"/>  
  <role rolename="role1"/>  
  <user username="tomcat" password="tomcat" roles="tomcat"/>  
  <user username="both" password="tomcat" roles="tomcat,role1"/>  
  <user username="role1" password="tomcat" roles="role1"/>  
-->  
</tomcat-users>

wie kann ich per Linux-Befehle (bash) die Kommentare entfernen und 2 rolenname und 1 username hinzufügen?

sed -i "s|^<!--||g" /etc/tomcat6/tomcat-users.xml
sed -i "s|^-->||g" /etc/tomcat6/tomcat-users.xml

Die Datei sollte dann ungefähr so aussehen:

<tomcat-users>  
 <role rolename="admin"/>  
 <role rolename="manager"/>  
 <role rolename="tomcat"/>  
 <role rolename="role1"/>  
 <user username="USER" password="PASSWORD" roles="admin,manager"/>  
 <user username="tomcat" password="tomcat" roles="tomcat"/>  
 <user username="both" password="tomcat" roles="tomcat,role1"/>  
 <user username="role1" password="tomcat" roles="role1"/>  
</tomcat-users>

Stella

  1. Hallo!

    die original XML-Datei sieht wie folgt aus:

    <?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.  
    -->  
    <tomcat-users>  
    <!--  
      <role rolename="tomcat"/>  
      <role rolename="role1"/>  
      <user username="tomcat" password="tomcat" roles="tomcat"/>  
      <user username="both" password="tomcat" roles="tomcat,role1"/>  
      <user username="role1" password="tomcat" roles="role1"/>  
    -->  
    </tomcat-users>
    

    und soll nach der Bearbeitung wie folgt aussehen

    <?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.  
    -->  
    <tomcat-users>  
      <role rolename="admin"/>  
      <role rolename="manager"/>  
      <role rolename="tomcat"/>  
      <role rolename="role1"/>  
      <user username="oscar" password="oscar" roles="admin,manager"/>  
      <user username="tomcat" password="tomcat" roles="tomcat"/>  
      <user username="both" password="tomcat" roles="tomcat,role1"/>  
      <user username="role1" password="tomcat" roles="role1"/>  
    </tomcat-users>
    

    Stella

    1. Hallo!

      es wäre auch ok, wenn die Zeilen

      <tomcat-users>  
      <!--  
        <role rolename="tomcat"/>  
        <role rolename="role1"/>  
        <user username="tomcat" password="tomcat" roles="tomcat"/>  
        <user username="both" password="tomcat" roles="tomcat,role1"/>  
        <user username="role1" password="tomcat" roles="role1"/>  
      -->  
      </tomcat-users>
      

      duech die Zeilen

      <tomcat-users>  
        <role rolename="admin"/>  
        <role rolename="manager"/>  
        <role rolename="tomcat"/>  
        <role rolename="role1"/>  
        <user username="oscar" password="oscar" roles="admin,manager"/>  
        <user username="tomcat" password="tomcat" roles="tomcat"/>  
        <user username="both" password="tomcat" roles="tomcat,role1"/>  
        <user username="role1" password="tomcat" roles="role1"/>  
      </tomcat-users>
      

      ersetzt werden.

      Mir fehlt jedoch der Ansatz dafür.

      Stella

      1. Tach,

        es wäre auch ok, wenn die Zeilen [...] duech die Zeilen [...] ersetzt werden.

        Mir fehlt jedoch der Ansatz dafür.

        sed arbeitet zeilenorientiert, für mehrzeiliges Suchen und Ersetzen könntest du z.B. awk nutzen.

        mfg
        Woodfighter

        1. Hallo Jens!

          es wäre auch ok, wenn die Zeilen [...] duech die Zeilen [...] ersetzt werden.

          Mir fehlt jedoch der Ansatz dafür.

          sed arbeitet zeilenorientiert, für mehrzeiliges Suchen und Ersetzen könntest du z.B. awk nutzen.

          ich suche jetzt seit ca. 15 Minuten nach ein Beispiel und finde leider nichts, was mich weiter bringt, z.B. How to Use Awk to Find and Replace Text

          Bisher habe ich awk nur für folgendes "missbraucht" "|awk '{print $4}'" in Kombination mit substr und length.

          Ein Beispiel, wie ich "<tag>.*</tag>" durch etwas anderen ersetzen kann, wäre super :-)

          Stella

            1. Hi Jens!

              http://www.unix.com/shell-programming-scripting/22057-search-replace-multi-line-text-files.html sollte helfen.

              so ganz hilft mir das leider nicht

              stella:~ # search="<tomcat-users>.*</tomcat-users>"
              stella:~ # replace="

              test1
              test2
              test3"

              stella:~ # awk ' BEGIN { RS="" }
                    FILENAME==ARGV[1] { s=$0 }
                    FILENAME==ARGV[2] { r=$0 }
                    FILENAME==ARGV[3] { sub(s,r) ; print }
                  ' $search $replace /etc/tomcat6/tomcat-users.xml
              awk: Fatal: Kann Datei '<tomcat-users>.*</tomcat-users>' nicht zum Lesen öffnen (Datei oder Verzeichnis nicht gefunden).

              Wie kann bzw. muss ich awk sagen, dass der erste und zweite Parameter ein String ist?

              Stella