V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
tikazyq
V2EX  ›  Linux

请教 sed 替换修改 xml 文件问题

  •  
  •   tikazyq ·
    tikazyq · 2015-10-16 19:14:11 +08:00 · 6675 次点击
    这是一个创建于 3350 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想用 shell 修改 solr 的配置文件

    <!--
    <env-entry>
        <env-entry-name>solr/home</env-entry-name>
        <env-entry-value>/path/to/solr_home</env-entry-value>
        <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    -->
    

    替换成

    <env-entry>
        <env-entry-name>solr/home</env-entry-name>
        <env-entry-value>/usr/solr/server/solr</env-entry-value>
        <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    

    将 comment 标签删掉,然后把 env-entry-value 中的值替换掉?

    请帮助一下我,是否能用 sed 达成目标?

    谢谢啦!

    15 条回复    2015-11-02 06:53:09 +08:00
    uuspider
        1
    uuspider  
       2015-10-16 22:32:32 +08:00   ❤️ 1
    sed -e '/^<!--$/d' -e '/^-->$/d' -e 's/<env-entry-value>\/path\/to\/solr_home<\/env-entry-value>/<env-entry-value>\/usr\/solr\/server\/solr<\/env-entry-value>/'

    还可以更简洁一点
    rrfeng
        2
    rrfeng  
       2015-10-16 22:48:45 +08:00   ❤️ 1
    用 sed 操作 xml 纯粹找麻烦……
    写个 python 能省一半时间还不容易出错。
    maskerTUI
        3
    maskerTUI  
       2015-10-16 22:51:52 +08:00 via Android
    为什么不用 vi 呢?
    cxbig
        4
    cxbig  
       2015-10-16 23:01:18 +08:00
    @uuspider 你那两个去掉 comments 的有点危险,万一前后有空格就匹配不上。
    另外不用分 3 条,可以用分号(;)分开: sed -e 's/foo1/bar1/;s/foo2/bar2/;s/foo3/bar3/'
    tikazyq
        5
    tikazyq  
    OP
       2015-10-16 23:07:10 +08:00
    @rrfeng 的确可以用 shell 调用 python 操作,大好,谢谢!
    tikazyq
        6
    tikazyq  
    OP
       2015-10-16 23:07:55 +08:00
    @maskerTUI 因为要用 vagrant 自动生成集群,用 vim 就不方便了
    henryon
        7
    henryon  
       2015-10-17 16:51:05 +08:00
    文本能贴全不?
    tikazyq
        8
    tikazyq  
    OP
       2015-10-17 18:20:21 +08:00
    @rrfeng 多谢,现在已经用 python re 搞定了

    python <<EOF
    import re
    fpath = '/usr/tomcat/webapps/solr/WEB-INF/web.xml'
    rep_str = '''
    <env-entry>
    <env-entry-name>solr/home</env-entry-name>
    <env-entry-value>/usr/solr/server/solr</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    '''
    with open(fpath) as f: text = f.read()
    text = re.sub('<!--\s+<env-entry>.*</env-entry>\s+-->', rep_str, text, flags=re.DOTALL)
    with open(fpath, 'wb') as f: f.write(text)
    EOF
    tikazyq
        9
    tikazyq  
    OP
       2015-10-17 18:21:48 +08:00
    @henryon

    <?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.
    -->
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5"
    metadata-complete="true"
    >


    <!-- Uncomment if you are trying to use a Resin version before 3.0.19.
    Their XML implementation isn't entirely compatible with Xerces.
    Below are the implementations to use with Sun's JVM.
    <system-property javax.xml.xpath.XPathFactory=
    "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl"/>
    <system-property javax.xml.parsers.DocumentBuilderFactory=
    "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"/>
    <system-property javax.xml.parsers.SAXParserFactory=
    "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"/>
    -->

    <!-- People who want to hardcode their "Solr Home" directly into the
    WAR File can set the JNDI property here...
    -->
    <!--
    <env-entry>
    <env-entry-name>solr/home</env-entry-name>
    <env-entry-value>/usr/solr/server/solr</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    -->

    <!-- Any path (name) registered in solrconfig.xml will be sent to that filter -->
    <filter>
    <filter-name>SolrRequestFilter</filter-name>
    <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>
    <!--
    Exclude patterns is a list of directories that would be short circuited by the
    SolrDispatchFilter. It includes all Admin UI related static content.
    NOTE: It is NOT a pattern but only matches the start of the HTTP ServletPath.
    -->
    <init-param>
    <param-name>excludePatterns</param-name>
    <param-value>/css/.+,/js/.+,/img/.+,/tpl/.+</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <!--
    NOTE: When using multicore, /admin JSP URLs with a core specified
    such as /solr/coreName/admin/stats.jsp get forwarded by a
    RequestDispatcher to /solr/admin/stats.jsp with the specified core
    put into request scope keyed as "org.apache.solr.SolrCore".

    It is unnecessary, and potentially problematic, to have the SolrDispatchFilter
    configured to also filter on forwards. Do not configure
    this dispatcher as <dispatcher>FORWARD</dispatcher>.
    -->
    <filter-name>SolrRequestFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
    <servlet-name>Zookeeper</servlet-name>
    <servlet-class>org.apache.solr.servlet.ZookeeperInfoServlet</servlet-class>
    </servlet>

    <servlet>
    <servlet-name>LoadAdminUI</servlet-name>
    <servlet-class>org.apache.solr.servlet.LoadAdminUiServlet</servlet-class>
    </servlet>

    <!-- Remove in Solr 5.0 -->
    <!-- This sends SC_MOVED_PERMANENTLY (301) for resources that changed in 4.0 -->
    <servlet>
    <servlet-name>RedirectOldAdminUI</servlet-name>
    <servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>
    <init-param>
    <param-name>destination</param-name>
    <param-value>${context}/#/</param-value>
    </init-param>
    </servlet>

    <servlet>
    <servlet-name>RedirectOldZookeeper</servlet-name>
    <servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>
    <init-param>
    <param-name>destination</param-name>
    <param-value>${context}/zookeeper</param-value>
    </init-param>
    </servlet>

    <servlet>
    <servlet-name>RedirectLogging</servlet-name>
    <servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>
    <init-param>
    <param-name>destination</param-name>
    <param-value>${context}/#/~logging</param-value>
    </init-param>
    </servlet>

    <servlet>
    <servlet-name>SolrRestApi</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
    <param-name>org.restlet.application</param-name>
    <param-value>org.apache.solr.rest.SolrSchemaRestApi</param-value>
    </init-param>
    </servlet>

    <servlet-mapping>
    <servlet-name>RedirectOldAdminUI</servlet-name>
    <url-pattern>/admin/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>RedirectOldAdminUI</servlet-name>
    <url-pattern>/admin</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>RedirectOldZookeeper</servlet-name>
    <url-pattern>/zookeeper.jsp</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>RedirectLogging</servlet-name>
    <url-pattern>/logging</url-pattern>
    </servlet-mapping>

    <!-- Servlet Mapping -->
    <servlet-mapping>
    <servlet-name>Zookeeper</servlet-name>
    <url-pattern>/zookeeper</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>LoadAdminUI</servlet-name>
    <url-pattern>/admin.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>SolrRestApi</servlet-name>
    <url-pattern>/schema/*</url-pattern>
    </servlet-mapping>

    <mime-mapping>
    <extension>.xsl</extension>
    <!-- per http://www.w3.org/TR/2006/PR-xslt20-20061121/ -->
    <mime-type>application/xslt+xml</mime-type>
    </mime-mapping>

    <welcome-file-list>
    <welcome-file>admin.html</welcome-file>
    </welcome-file-list>

    </web-app>
    rrfeng
        10
    rrfeng  
       2015-10-17 19:34:46 +08:00
    用 python 就是让你别用 re ……
    tikazyq
        11
    tikazyq  
    OP
       2015-10-18 01:01:30 +08:00
    @rrfeng 那咋弄啊?
    uuspider
        12
    uuspider  
       2015-10-18 07:06:30 +08:00
    @tikazyq python 有处理 xml 的类库、模块
    henryon
        13
    henryon  
       2015-10-19 18:10:01 +08:00
    sed -i -e '/^<!--$/ {
    N; /\n<env-entry>$/ {
    N; /\n-->$/ {
    s/^<!--\n//; s/\n-->$//
    }
    }
    }' urfile
    rootit
        14
    rootit  
       2015-10-31 21:12:24 +08:00
    sed 最好解决了
    Arthur2e5
        15
    Arthur2e5  
       2015-11-02 06:53:09 +08:00
    为什么要螳臂挡车地飞正则处理 XML ……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4070 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 05:25 · PVG 13:25 · LAX 21:25 · JFK 00:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.