-2

I want to do match multiple continuous lines in a file and then replace it with new multiple continuous lines. For example:

Pattern:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

to be replaced with :

<Connector port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
         acceptCount="450" maxConnections="10000"
           redirectPort="8443" server=" " />

Even though here few keywords are common in both the paragraph but just consider them different.

I tried with following but could not get success:

sed -i 's/<Connector port="8080" protocol="HTTP\/1.1" \nconnectionTimeout="20000" \nredirectPort="8443" \/>/<Connector port="8080" protocol="HTTP\/1.1" \nconnectionTimeout="20000" \nacceptCount="450" maxConnections="10000" \nredirectPort="8443" server=" " \/>/g' server.xml

How can i achieve above.

Thanks.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 3
    That's not a paragraph, it's an XML tag. Use tools that understand XML and are intended to manipulate it. – Shawn Aug 07 '21 at 20:35
  • Thanks Shawn for your prompt reply. But i want to do it using sed or awk or grep. Can we do this without any XML tool. – SAKA RAM DEWASI Aug 07 '21 at 20:45
  • 3
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Aug 07 '21 at 20:46
  • 2
    Wanting to use things like sed to work with XML is like using a hammer to drive a screw. Use the right tool for the job. – Shawn Aug 07 '21 at 20:48
  • Thanks Cyrus. I cant use such tools. I want to do this processing using bash/shell script. – SAKA RAM DEWASI Aug 07 '21 at 20:49
  • 1
    If you want to use tools that most professionals would consider totally unsuited to the task, at least explain why you want to do so. Remember, we see lots of posts every day from people wanting to know how to cope with invalid XML, which is the inevitable result of people generating XML using the wrong tools. – Michael Kay Aug 08 '21 at 08:54

2 Answers2

0

You can try this sed or awk although a tool made for the job may be better.

sed -E '/[a-zA-Z]*."20000"/ {N;s|[a-z]*"8443".*|acceptCount="450" maxConnections="10000"\n\t\tredirectPort="8443" server=" " />|}'
awk 'NR==3{$0="\t\tacceptCount=\"450\" maxConnections=\"10000\"\n\t\tredirectPort=\"8443\" server=\" \"/>"}1'
HatLess
  • 10,622
  • 5
  • 14
  • 32
0

Option -z of sed allows to replace multiline block easily :

sed -E -z 's!(<Connector port="8080" protocol="HTTP/1.1"\
\s+connectionTimeout="20000"\
\s+)(redirectPort="8443")!\1acceptCount="450" maxConnections="10000"\
           \2 server=" "!'   inputfile
Philippe
  • 20,025
  • 2
  • 23
  • 32