我想使用" grep / awk"来剪切文本文件。或其他类似的工具,例如" grep代理data.txt"然后我得到了" 15988",从与我想要找到的模式相关联的段落中获得的数字(这实际上是端口号)。通过使用" ============"扩展任何扩展的端口。字符
有些相似,但我变得头晕目眩 How can I make 'grep' show a single line five lines above the grepped line?==============NEXT SERVICE FINGERPRINT (SUBMIT INDIVIDUALLY)==============
SF-Port15988-TCP:V=7.01%I=7%D=8/3%Time=57A107C3%P=i586-pc-linux-gnu%r(GetR
SF:equest,24E,"HTTP/1\.1\x20404\x20Not\x20found\r\nConnection:\x20close\r\
SF:nDate:\x20Tue,\x2002\x20Aug\x202016\x2020:44:05\x20GMT\r\nContent-Type:
SF:\x20text/html\r\nContent-Length:\x20407\r\nExpires:\x200\r\nCache-Contr
SF:ol:\x20no-cache\r\nPragma:\x20no-cache\r\n\r\n<!DOCTYPE\x20HTML\x20PUBL
SF:IC\x20\"-//W3C//DTD\x20HTML\x204\.01\x20Transitional//EN\"\x20\"http://
SF:www\.w3\.org/TR/html4/loose\.dtd\">\n<html><head>\n<title>Proxy\x20erro
SF:r:\x20404\x20Not\x20found\.</title>\n</head><body>\n<h1>404\x20Not\x20f
SF:ound</h1>\n<p>The\x20following\x20error\x20occurred\x20while\x20trying\
SF:x20to\x20access\x20<strong>/</strong>:<br><br>\n<strong>404\x20Not\x20f
SF:ound</strong></p>\n<hr>Generated\x20Wed,\x2003\x20Aug\x202016\x2003:44:
SF:05\x20WIT\x20by\x20rpc\x20on\x20<em>sevdev:15988</em>\.\n</body></html>
SF:\r\n")%r(HTTPOptions,258,"HTTP/1\.1\x20501\x20Method\x20not\x20implemen
SF:ted\r\nConnection:\x20close\r\nDate:\x20Tue,\x2002\x20Aug\x202016\x2020
SF::44:05\x20GMT\r\nContent-Type:\x20text/html\r\nContent-Length:\x20404\r
SF:\nExpires:\x200\r\nCache-Control:\x20no-cache\r\nPragma:\x20no-cache\r\
SF:n\r\n<!DOCTYPE\x20HTML\x20PUBLIC\x20\"-//W3C//DTD\x20HTML\x204\.01\x20T
SF:ransitional//EN\"\x20\"http://www\.w3\.org/TR/html4/loose\.dtd\">\n<htm
SF:l><head>\n<title>Proxy\x20error:\x20501\x20Method\x20not\x20implemented
SF:\.</title>\n</head><body>\n<h1>501\x20Method\x20not\x20implemented</h1>
SF:\n<p>The\x20following\x20error\x20occurred:<br><br>\n<strong>501\x20Met
SF:hod\x20not\x20implemented</strong></p>\n<hr>Generated\x20Wed,\x2003\x20
SF:Aug\x202016\x2003:44:05\x20WIT\x20by\x20rpc\x20on\x20<em>sevdev:15988</
SF:em>\.\n</body></html>\r\n")%r(RTSPRequest,26C,"HTTP/1\.1\x20400\x20Erro
SF:r\x20in\x20first\x20request\x20line\r\nConnection:\x20close\r\nDate:\x2
SF:0Tue,\x2002\x20Aug\x202016\x2020:44:05\x20GMT\r\nContent-Type:\x20text/
SF:html\r\nContent-Length:\x20419\r\nExpires:\x200\r\nCache-Control:\x20no
SF:-cache\r\nPragma:\x20no-cache\r\n\r\n<!DOCTYPE\x20HTML\x20PUBLIC\x20\"-
SF://W3C//DTD\x20HTML\x204\.01\x20Transitional//EN\"\x20\"http://www\.w3\.
SF:org/TR/html4/loose\.dtd\">\n<html><head>\n<title>Proxy\x20error:\x20400
SF:\x20Error\x20in\x20first\x20request\x20line\.</title>\n</head><body>\n<
SF:h1>400\x20Error\x20in\x20first\x20request\x20line</h1>\n<p>The\x20follo
SF:wing\x20error\x20occurred:<br><br>\n<strong>400\x20Error\x20in\x20first
SF:\x20request\x20line</strong></p>\n<hr>Generated\x20Wed,\x2003\x20Aug\x2
SF:02016\x2003:44:05\x20WIT\x20by\x20rpc\x20on\x20<em>sevdev:15988</em>\.\
SF:n</body></html>\r\n");
我已经完成了
grep "Proxy" aaa.txt
结果(我知道我在做什么):
SF: www \ .w3 \ .org / TR / HTML4 / loose \ .dtd \ "> \ n <html> <head> \ n <title> Proxy \ x20erro
SF: l> <head> \ n <title> Proxy \ x20error: \ x20501 \ x20Method \ x20not \ x20implemented
SF: org / TR / HTML4 / loose \ .dtd \ "> \ n <html> <head> \ n <title> Proxy \ x20error: \ x20400
只是想知道如何剪切/ grep所以结果变成这样:
15988
15988
15988
谢谢,
答案 0 :(得分:2)
通常,要在满足 ConditionB 的行之前获得满足 ConditionA 的最后一行,您可以这样做:
awk 'ConditionA {last=$0} ConditionB {print last}'
如果 ConditionA 和 ConditionB 可能同时为true,则需要更准确地定义您希望如何处理该案例。
但是,查看示例输入,似乎在您的情况下:
ConditionA 可能是&#34;不是以SF:
&#34; !/^SF:/
中的awk
。
ConditionB 可能是&#34;以SF:
开头,包含字符串Proxy
&#34; /^SF:.*Proxy/
awk
将这些放在一起,你得到:
awk '!/^SF:/ {last=$0} /^SF:.*Proxy/ {print last}'
你可以改写为:
awk '!/^SF:/ {last=$0; next} /Proxy/ {print last}'
答案 1 :(得分:1)
这是你想要做的吗?
$ awk 'match($0,/^SF-Port([0-9]+).*/,a){port=a[1]} /Proxy/{print port}' file
15988
15988
15988
上面使用GNU awk作为第3个arg来匹配(),与其他awks它是几个sub()s或类似的隔离端口号,例如:
$ awk 'sub(/^SF-Port/,""){sub(/[^0-9].*/,""); port=$0} /Proxy/{print port}' file
15988
15988
15988
或:
$ awk 'match($0,/^SF-Port[0-9]+/){port=substr($0,8,RLENGTH-7)} /Proxy/{print port}' file
15988
15988
15988