Basically, I have output from nmap, which gives me an IP and then a list of open ports underneath that, followed by a blank line. I have filtered out the ports that I don't want anymore (grep -v http
, for example), but I can't figure out how to remove the IP addesses that have no following ports.
Is there a way to do this with sed?
Sample data:
Nmap scan report for 1.1.1.1
3389/tcp open ms-term-serv
5357/tcp open unknown
5432/tcp open postgresql
8080/tcp open http-proxy
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
Nmap scan report for 2.2.2.2
Nmap scan report for 3.3.3.3
80/tcp open http
443/tcp open https
6646/tcp open unknown
8000/tcp open http-alt
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49156/tcp open unknown
49157/tcp open unknown
With awk
you can simply do awk '$2' ORS='\n\n' FS='\n' RS= file
:
$ awk '$2' ORS='\n\n' FS='\n' RS= file
Nmap scan report for 1.1.1.1
3389/tcp open ms-term-serv
5357/tcp open unknown
5432/tcp open postgresql
8080/tcp open http-proxy
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
Nmap scan report for 3.3.3.3
80/tcp open http
443/tcp open https
6646/tcp open unknown
8000/tcp open http-alt
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49156/tcp open unknown
49157/tcp open unknown
If the extra newline added to the end of file in previous script is a problem then use this alternative:
awk '/^Nmap/{h=$0;i=NR;next}NR==i+1{if($0){print h;p=1}else p=0}p' file