Sed print only pattern match

nmapregexsed

I am trying to get versions for services in a gnmap file. A typical line looks like:

Host: 192.x.x.x ()    Ports: 21/open/tcp//ftp//HP JetDirect ftpd/, 23/open/tcp//telnet//HP JetDirect printer telnetd (No password)/, 80/open/tcp//http//HP-ChaiSOE 1.0 (HP LaserJet http config)/, 443/open/tcp//ssl|http//HP-ChaiSOE 1.0 (HP LaserJet http config)/, 515/open/tcp//printer///, 631/open/tcp//http//HP-ChaiSOE 1.0 (HP LaserJet http config)/, 7627/open/tcp//http//HP-ChaiSOE 1.0 (HP LaserJet http config)/, 9100/open/tcp/////, 14000/open/tcp//tcpwrapped///    Seq Index: 25   IP ID Seq: Incremental

I need to match specific "open" ports and print only the matching expression. I have tried:

cat file | sed -n "/ 80\/open\/tcp\/\*\/\*\/\*\/\*\//p"

I need the result to be:

80/open/tcp//http//HP-ChaiSOE 1.0 (HP LaserJet http config)/

Best Answer

You can do the same using grep with the -o (--only-matching) option:

cat teste | grep -o "80\/open\/tcp\/[^,]*"
Related Question