问题描述
我需要将nmap输出中的多行合并为一行
i need to merge multiple lines from the output of nmap into a single row
从:
Nmap scan report for example.com
22/tcp open ssh
80/tcp open http
111/tcp open rpcbind
1720/tcp open h323q931
5432/tcp open postgresql
Nmap scan report for example.com
22/tcp open ssh
80/tcp open http
81/tcp open hosts2-ns
111/tcp open rpcbind
1720/tcp open h323q931
Nmap scan report for example.com
22/tcp open ssh
111/tcp open rpcbind
1720/tcp open h323q931
Nmap scan report for example.com
22/tcp open ssh
收件人:
Nmap scan report for example.com
22/tcp open ssh 80/tcp open http 111/tcp open rpcbind 1720/tcp open h323q931 5432/tcp open postgresql
Nmap scan report for example.com
22/tcp open ssh 80/tcp open http 81/tcp open hosts2-ns 111/tcp open rpcbind 1720/tcp open h323q931
Nmap scan report for example.com
22/tcp open ssh 111/tcp open rpcbind 1720/tcp open h323q931
Nmap scan report for example.com
22/tcp open ssh
对于1个主机,应该有2行.第一个从Nmap扫描开始...,另一行是打开的端口信息.我看到许多关于多行与awk合并为1行的问题,但无法解决问题.
For 1 host there should be 2 lines. 1st one is starting with Nmap scan ... and the other row is open port info. I saw many questions about multiple lines into 1 line with awk but could'not come up with something.
推荐答案
为什么不这样做:
awk '$0~"Nmap"{if(output!="")print output;print;output=""}$0!~"Nmap"{output=output""$0" "}END{if(output!="")print output}'
如果$ 0具有"Nmap",则打印该行.如果没有,请跟踪正在发生的事情,然后在新的Nmap时将其打印出来.
If $0 has "Nmap", print that line. If not, keep track of what is happening, then print it when a new Nmap.
END{}
块负责触发未打印的内容.请注意,您正在寻找Nmap
来打印出一些东西.另请注意条件$0!~"Nmap"
,以避免同样缓冲第一行.
The END{}
block takes care of firing what was not printed. Note that you are looking for Nmap
to print something out. Also note the condition $0!~"Nmap"
to avoid buffering that first line as well.
这篇关于使用awk(或熟悉的)将多行合并为1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!