简体   繁体   中英

sed pattern find and replace lines

I need to use sed to search for This line

<Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443" />

and replace it with a new 3 lines

<Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443"
maxThreads="300" minSpareThreads="25" maxSpareThreads="75" acceptCount="100"
enableLookups="false"  connectionTimeout="60000" />

I used the following pattern , but i believe i missing escaping part

sed -i 's/ <Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443" />/<Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443"
        maxThreads="300" minSpareThreads="25" maxSpareThreads="75" acceptCount="100"
        enableLookups="false"  connectionTimeout="60000" />/g'

Advise please

Problem is that your search string does contain / . I make it part of the sed code.
Try

sed -i 's|search|replace|g' file

And your sed search for a space before the string. Need to be removed:

s/ <

correct

s/<

Complete command:

sed -i 's|<Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443" />|<Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443"     maxThreads="300" minSpareThreads="25" maxSpareThreads="75" acceptCount="100" enableLookups="false"  connectionTimeout="60000" />|g' file

Either, backslash the slashes in the search string (you should also do it for dots to prevent them from matching any character).

Or, use a proper XML handling tool to process XML data. For example, xsh :

open file.xml ;
for //Connector[@URIEncoding="UTF-8" and @port="8009"
                and @protocol="AJP/1.3" and @redirectPort="8443"] {
    set @maxThreads 300 ;
    set @minSpareThreads 25 ;
    set @enableLookups 'false' ;
    # etc.
}
save :b ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM