如何使用awk,perl或sed从LiveHTTPHeaders输出中删除响应?

Tim*_*Tim 2 linux perl firefox awk

假设我有这样的东西(这只是一个例子,实际的请求会有所不同:我加载了启用了LiveHTTPHeaders的StackOverflow,以便有一些样本可以处理):

http://stackoverflow.com/

GET / HTTP/1.1
Host: stackoverflow.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070220 Firefox/2.0.0.2
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Sat, 28 Nov 2009 16:04:24 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/7.0
Date: Sat, 28 Nov 2009 16:04:23 GMT
Content-Length: 19015
----------------------------------------------------------
...

pastebin上可以获得请求和响应的完整日志

我想删除所有响应(例如,HTTP/1.x 200 OK以及该响应中的所有内容)以及显示页面地址的所有内容.我想只保留文本文件中保存的LiveHTTPHeaders输出的所有请求.

所以,输出将是:

GET / HTTP/1.1
Host: stackoverflow.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070220 Firefox/2.0.0.2
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

GET /so/all.css?v=5290 HTTP/1.1
Host: sstatic.net
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070220 Firefox/2.0.0.2
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://stackoverflow.com/

...

同样,我想要保留的全文可以在pastebin上找到.

如果我将LiveHTTPHeaders捕获的会话保存到文本文件中,我希望在此问题中得到第二个"代码"的结果,我该怎么做?也许有awk,sedperl?或者是其他东西?我在Linux上.


编辑:我正在尝试运行思南的剧本.脚本是这样的:

#!/usr/bin/perl
local $/ = "\n\n";
while (<>) {
    print if /^GET|POST/; # Add more request types as needed
}
Run Code Online (Sandbox Code Playgroud)

我尝试这样运行:

./cleanup-headers.pl livehttp.txt > filtered.txt
Run Code Online (Sandbox Code Playgroud)

这样:

perl cleanup-headers.pl < livehttp.txt > filtered.txt
Run Code Online (Sandbox Code Playgroud)

...文件filtered.txt已创建,但它完全为空.

任何人都尝试在FULL标题上粘贴到pastebin?它有用吗?

完整标题

Sin*_*nür 5

在Perl中:

local $/ = "\n\n";
while (<>) {
    print if /^(?:GET|POST)/; # Add more request types as needed
}
Run Code Online (Sandbox Code Playgroud)

注意:查看LiveHTTPHeaders生成的输出,条目由两个换行符分隔开,所以我认为设置$/ = "\n\n"比设置更合适$/ = ''.我相信你的问题是由于输入文件中的行实际上是缩进的.

我最初从pastebin下载文件并使用完整文件来测试我的脚本.我不相信您在计算机上测试的文件与您在pastebin上使用的文件相同.

如果要在保持与LiveHTTPHeaders输出格式一致的情况下稳健地处理可能的缩进行,则应使用以下内容:

#!/usr/bin/perl

use strict; use warnings;

local $/ = "\n\n";
while (<>) {
    next unless /^\s*(?:GET|POST)/;
    s!^\s+!!gm;
    print;
}
Run Code Online (Sandbox Code Playgroud)

我认为使用sedperl在同一个管道中有点令人厌恶.