我尽我所能得到这个并没有运气,我有以下文件,我想要更换
第一个“<Connector port="8080"” xml block with ‘xxxx’
之前:
<!-- 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" />
-->
之后:
<!-- 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" />
-->
我使用以下sed命令打印匹配的字符串:
sed -n '/<Connector port="8080"/,/>/p' filename
然而,我无法将其发展为高于输出。
答案 0 :(得分:2)
此sed
应该对您的示例输入执行操作(但如果您有多个<Connector port="8080"
部分,则会过于热心):
sed '/<Connector port="8080"/,/>/{ s/<Connector.*/xxxx/; t; d }'
但是强有力地处理这个问题需要一个XML解析器。例如:
#!/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) }
答案 1 :(得分:0)
sed '/<Connector port="8080"/,/>/ {
N
/>/ s/.*/xxxx/
}' YourFile
在将连接器部分更改为xxxx之前加载连接器部分,这允许您最终在其上测试某些内容(但如果没有则优于pobrelkey)
答案 2 :(得分:0)
感谢大家的回答,他们运作良好。感谢它: 此外,我从我的一个朋友那里得到了关注,这也是使用awk的答案
awk '/<Connector port/ {
print "abc"
getline
do {
getline
} while (index($0, ">") == 0)
getline
}
// { print $0}' < 1.txt