0

I tried my best to get this and had little luck, I have following file and I want to replace first “<Connector port="8080"” xml block with ‘xxxx’

Before :

<!-- A "Connector" represents an endpoint by which requests are received
     and responses are returned. Documentation at :
     Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
     Java AJP  Connector: /docs/config/ajp.html
     APR (HTTP/AJP) Connector: /docs/apr.html
     Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
           port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />
--> 

After :

<!-- A "Connector" represents an endpoint by which requests are received
     and responses are returned. Documentation at :
     Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
     Java AJP  Connector: /docs/config/ajp.html
     APR (HTTP/AJP) Connector: /docs/apr.html
     Define a non-SSL HTTP/1.1 Connector on port 8080
-->
xxxx
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
           port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />
--> 

I got the matching string to print using following sed command :

sed -n '/<Connector port="8080"/,/>/p' filename

However I’m unable to develop this to get above output.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
csf
  • 529
  • 1
  • 4
  • 16
  • Be aware that replacing an element in XML with regular expressions (i.e. `sed`) is not possible for all general XML documents. With `sed`, you can only solve this problem in a subset of XML documents. For general XML processing, use a proper XML processing technology such as XSLT. – ndim Nov 14 '13 at 18:30
  • 1
    http://stackoverflow.com/a/1732454/1841533 – Olivier Dulac Nov 14 '13 at 18:35
  • 1
    @ndim: True. However, in this case it is not general xml, it is a very specific case, which does in deed work. – carlpett Nov 14 '13 at 19:43

3 Answers3

2

This sed should do the trick on your sample input (but gets overzealous if you have more than one <Connector port="8080" section):

sed '/<Connector port="8080"/,/>/{ s/<Connector.*/xxxx/; t; d  }'

But handling this robustly calls for an XML parser. Example:

#!/usr/bin/env ruby
require 'rexml/document'
include REXML
d = Document.new(File.read(ARGV[0]))
e = d.get_elements('//Connector[@port="8080"]').first
if !e.nil?
    e.parent.insert_after(e, Text.new('xxxx'))
    e.parent.delete(e)
end
File.open(ARGV[0],'w'){|f| f.print(d) }
pobrelkey
  • 5,853
  • 20
  • 29
  • Be aware the difference between _parsing_ and _searching_ xml. I'm all for not trying to parse xml using regex, but in this case it is not actually parsing that is asked for. – carlpett Nov 14 '13 at 19:44
0
sed  '/<Connector port="8080"/,/>/ {
   N
   />/ s/.*/xxxx/
  }' YourFile 

load the section of connector before changing it to xxxx, this allow you to eventually test something on it (but not better than pobrelkey if not)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
0

Thanks all for the answers , they worked well .Appriciate it: Also i got following from one of my firend which also a answer to this using awk

awk '/<Connector port/ {
print "abc"
getline
do {
 getline
} while (index($0, ">") == 0)
getline
} 
// { print $0}' < 1.txt
csf
  • 529
  • 1
  • 4
  • 16