2

I need to filter with sed only the ports from /usr/share/nmap/nmap-services

tcpmux  1/tcp   0.001995        # TCP Port Service Multiplexer [rfc-1078]
compressnet     2/tcp   0.000013        # Management Utility
compressnet     3/tcp   0.001242        # Compression Process
unknown 4/tcp   0.000477
unknown 6/tcp   0.000502
echo    7/tcp   0.004855
unknown 8/tcp   0.000013
discard 9/tcp   0.003764        # sink null
unknown 10/tcp  0.000063
systat  11/tcp  0.000075        # Active Users

I've tryed something like (!?([0-9]+/tcp)) But it wont work: why?

Thank you

asdf
  • 685
  • 7
  • 23

3 Answers3

2

Try doing this :

grep -oP '\d+(?=/(udp|tcp))' /usr/share/nmap/nmap-services

or with perl :

perl -lne 'print $& if m!\d+(?=/(udp|tcp))!' /usr/share/nmap/nmap-services

I use a positive look ahead advanced regex, see http://www.perlmonks.org/?node_id=518444

or with awk without advanced regex :

awk '{gsub("/.*", ""); print $2}' /usr/share/nmap/nmap-services

or

awk -F'[ /\t]' '{print $2}' /usr/share/nmap/nmap-services
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • wont work.. i've partially restrict the problem with cat /usr/share/nmap/nmap-services | grep '[0-9]/tcp' – asdf Nov 15 '12 at 03:18
0

Here's an example using AWK

cat /usr/share/nmap/nmap-services | awk '{print $2}' | awk -F\/ '{print $1}'
RockWad
  • 15
  • 2
  • 6
0

The simplest is so:

cut -s -d\  -f2 test

You can also do it so:

sed '/[^ ]* \([^ ]*\).*/ s::\1:; /^$/d' FILE

cut variant prints empty lines for non-matching.

alinsoar
  • 15,386
  • 4
  • 57
  • 74