我有一个像这样的 Nmap 输出文件:
Nmap scan report for AAAAAAAAAAAAAAAAAA.akamaitechnologies.com (IP)
Host is up, received user-set (0.027s latency).
Scanned at 2016-10-09 14:43:24 CEST for 15s
PORT STATE SERVICE REASON VERSION
443/tcp open ssl/http syn-ack ttl 59 AkamaiGHost (Akamai's HTTP Acceleration/Mirror service)
Nmap scan report for AAAAAAAAAAAAAAAAAA.akamaitechnologies.com (IP)
Host is up, received user-set.
Scanned at 2016-10-09 14:43:21 CEST for 2s
PORT STATE SERVICE REASON VERSION
2222/tcp filtered EtherNetIP-1 no-response
Nmap scan report for CCCCCCCCCCC. googleusercontent.com (IP)
Host is up, received user-set (0.15s latency).
Scanned at 2016-10-09 14:43:21 CEST for 3s
PORT STATE SERVICE REASON VERSION
2222/tcp open ssh syn-ack ttl 55 ProFTPD mod_sftp 0.9.9 (protocol 2.0)
Nmap scan report for BBBBBBBBBBBBBBBBBBB
Host is up, received user-set (0.16s latency).
Scanned at 2016-10-09 14:43:21 CEST for 3s
PORT STATE SERVICE REASON VERSION
2222/tcp open ssh syn-ack ttl 51 ProFTPD mod_sftp 0.9.9 (protocol 2.0)
Nmap scan report for AAAAAAAAAAAAAAAAAA.akamaitechnologies.com (IP)
Host is up, received user-set (0.027s latency).
Scanned at 2016-10-09 14:43:24 CEST for 15s
PORT STATE SERVICE REASON VERSION
443/tcp open ssl/http syn-ack ttl 59 AkamaiGHost (Akamai's HTTP Acceleration/Mirror service)
我想使用排序匹配“Nmap 扫描报告”对每 5 行块对此文件进行排序,输出应该是:
Nmap scan report for AAAAAAAAAAAAAAAAAA.akamaitechnologies.com (IP)
Host is up, received user-set (0.027s latency).
Scanned at 2016-10-09 14:43:24 CEST for 15s
PORT STATE SERVICE REASON VERSION
443/tcp open ssl/http syn-ack ttl 59 AkamaiGHost (Akamai's HTTP Acceleration/Mirror service)
Nmap scan report for AAAAAAAAAAAAAAAAAA.akamaitechnologies.com (IP)
Host is up, received user-set.
Scanned at 2016-10-09 14:43:21 CEST for 2s
PORT STATE SERVICE REASON VERSION
2222/tcp filtered EtherNetIP-1 no-response
Nmap scan report for BBBBBBBBBBBBBBBBBBB
Host is up, received user-set (0.16s latency).
Scanned at 2016-10-09 14:43:21 CEST for 3s
PORT STATE SERVICE REASON VERSION
2222/tcp open ssh syn-ack ttl 51 ProFTPD mod_sftp 0.9.9 (protocol 2.0)
Nmap scan report for CCCCCCCCCCC. googleusercontent.com (IP)
Host is up, received user-set (0.15s latency).
Scanned at 2016-10-09 14:43:21 CEST for 3s
PORT STATE SERVICE REASON VERSION
2222/tcp open ssh syn-ack ttl 55 ProFTPD mod_sftp 0.9.9 (protocol 2.0)
使用unix命令的解决方案值得赞赏。
答案1
对于每个五行块连接行(例如,使用低 ascii 字符,就像\x02
您的输入中不太可能出现的那样 - 假设您的工具支持它),对第一个字段进行排序,然后恢复换行符:
sed 'N;N;N;N;s/\n/\x02/g' infile | sort -t$'\02' -k1,1 | tr '\02' '\n'