Linux 使用awk(或熟悉的)将多行合并为一行

Linux 使用awk(或熟悉的)将多行合并为一行,linux,bash,awk,Linux,Bash,Awk,我需要将nmap输出中的多行合并到一行中 发件人: 致: 对于1台主机,应该有2行。第一个是从Nmap扫描开始。。。另一行是开放端口信息。我看到很多关于用awk将多行转换成一行的问题,但我想不出什么来。为什么不这样做: awk '$0~"Nmap"{if(output!="")print output;print;output=""}$0!~"Nmap"{output=output""$0&quo

我需要将nmap输出中的多行合并到一行中

发件人:

致:

对于1台主机,应该有2行。第一个是从Nmap扫描开始。。。另一行是开放端口信息。我看到很多关于用awk将多行转换成一行的问题,但我想不出什么来。

为什么不这样做:

awk '$0~"Nmap"{if(output!="")print output;print;output=""}$0!~"Nmap"{output=output""$0" "}END{if(output!="")print output}'
如果$0有“Nmap”,请打印该行。如果没有,请跟踪正在发生的事情,然后在新的Nmap出现时打印它


END{}
块负责激发未打印的内容。请注意,您正在寻找
Nmap
以打印某些内容。还要注意条件
$0~“Nmap”
以避免缓冲第一行。

请您尝试使用GNU
awk
中显示的示例编写并测试以下内容

awk '{printf("%s%s%s",$0~/^[0-9]/?"":(FNR!=1?ORS:""),$0,$0~/^Nmap/?ORS:"")} END{print ""}' Input_file
或非一行格式,以便更好地理解

awk '
{
  printf("%s%s%s",$0~/^[0-9]/?"":\
        (FNR!=1?ORS:""),$0,\
        $0~/^Nmap/?ORS:"")
}
END{ print "" }
' Input_file
说明:为上述解决方案添加详细说明仅用于理解目的,不用于运行

awk '                                    ##Starting awk program from here.
{                                        ##Using printf for checking 3 conditions and as per that print things.
  printf("%s%s%s",$0~/^[0-9]/?"":\       ##First checking if a line starts with digit then print NULL OR if its first line print NULL else print new line.
        (FNR!=1?ORS:""),$0,\             ##Checking condition if line is NOT first line then print new line else print NULL along with current line.
        $0~/^Nmap/?ORS:"")               ##Checking condition if line starts from Nmap then print new line or print NULL here.
}
END{ print "" }                          ##Staring END block of this code here and printing null line here.
' Input-file                             ##Mentioning Input_file name here.

使用GNU sed的替代方案:

$ sed -n '1{h;d}; /^Nmap/{x;s/\n/ /2g;p;d}; H; ${x;s/\n/ /2g;p}' input.txt
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

请回答您的问题,向我们展示您迄今为止为自己解决问题所做的努力。看。我不能通过流编辑器来做任何事情,然后删除换行符,比如
sed-zu的/\(\n\)\([[:digit:]\)/\2/g'
(仅GNU sed)是的,它也可以,你能解释一下发生了什么吗?@emrekirati,当然,请给我几分钟时间,我会为同样的事情添加详细的解释(为晚餐跑步):@emrekirati,增加了以上的详细解释,请检查相同的,让我知道在任何疑问的情况下干杯。
awk '                                    ##Starting awk program from here.
{                                        ##Using printf for checking 3 conditions and as per that print things.
  printf("%s%s%s",$0~/^[0-9]/?"":\       ##First checking if a line starts with digit then print NULL OR if its first line print NULL else print new line.
        (FNR!=1?ORS:""),$0,\             ##Checking condition if line is NOT first line then print new line else print NULL along with current line.
        $0~/^Nmap/?ORS:"")               ##Checking condition if line starts from Nmap then print new line or print NULL here.
}
END{ print "" }                          ##Staring END block of this code here and printing null line here.
' Input-file                             ##Mentioning Input_file name here.
$ sed -n '1{h;d}; /^Nmap/{x;s/\n/ /2g;p;d}; H; ${x;s/\n/ /2g;p}' input.txt
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