Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)/
share|improve this question

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

cat teste | grep -o "80\/open\/tcp\/[^,]*"
share|improve this answer

Here is one way,

sed 's|.*, 80\([^,]*\).*|80\1|' 

and, to avoid matching say '8080' here,

sed 's|.*, 80\/\([^,]*\).*|80/\1|' 

But, I think, there is a better output form of NMAP that will make your life easier.
And, you will find more exact ways for this grepable output in nmap documentation.

share|improve this answer
Actually a gnmap file is the grepable file. Grep grabs the whole line. I need only the output that I listed above. Unfortunately the command you listed does not result in printing the result I am looking for. – blanker Jun 15 '11 at 14:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.