[英]How to remove blocks starting and ending with certain lines from text file using Ruby?
I want to remove blocks from text file starting and ending with certain lines using Ruby. 我想使用Ruby从文本行中以某些行开始和结束的地方删除块。 I have generated text file containing HTTP request response but want to remove all responses leaving only headers in file.Response blocks start with reading all...
line and ends with Conn close
line. 我已经生成了包含HTTP请求响应的文本文件,但想删除所有响应,仅在文件中保留标题。响应块以reading all...
行开始,以Conn close
行结束。 How can I remove such blocks from my text file? 如何从文本文件中删除此类块? Example code is following: 示例代码如下:
opening connection to 209.85.175.121...
opened
<- "GET / HTTP/1.1\r\nConnection: close\r\nHost: 209.85.175.121\r\nOrigin: 192.168.100.111\r\n\r\n"
-> "HTTP/1.1 404 Not Found\r\n"
-> "Date: Thu, 23 Feb 2012 10:40:39 GMT\r\n"
-> "Content-Type: text/html; charset=UTF-8\r\n"
-> "Server: ghs\r\n"
-> "Content-Length: 931\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading 931 bytes...
-> "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}\n </style>\n <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>\n <p><b>404.</b> <ins>That\342\200\231s an error.</ins>\n <p>The requested URL <code>/</code> was not found on this server. <ins>That\342\200\231s all we know.</ins>\n"
read 931 bytes
Conn close
f = File.open("Access-Control-Allow-Origin.txt")
text = f.read
if text =~ /"Access-Control-Allow-Origin: *\r\n"/ then
puts "#########\n\nFound ---> Access-Control-Allow-Origin: *\n\n#########"
else
puts "#########\n\nNo Access-Control-Allow-Origin: * found\n\n#########"
end
Want to do inside this if condition. 要在此条件内做。
You can do it using ruby flip-flop operator: 您可以使用ruby 触发器运算符来做到这一点:
def remove_unnecessary_lines(filename)
lines = File.readlines(filename)
File.open(filename, 'w') do |f|
lines.each do |line|
unless line =~ /^reading all...$/..line =~ /^Conn close$/
f.puts line
end
end
end
end
UPDATE 更新
This is a different question, but, if I understood you right, you can put your code inside lines.each
(just before unless
): 这是一个不同的问题,但是,如果我理解正确的话,可以将代码放在lines.each
( unless
之前, unless
):
if line =~ /"Access-Control-Allow-Origin: *\r\n"/ then
puts "#########\n\nFound ---> Access-Control-Allow-Origin: *\n\n#########"
else
puts "#########\n\nNo Access-Control-Allow-Origin: * found\n\n#########"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.