asdf
asdf

Reputation: 698

Regexp on nmap services file

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

Upvotes: 2

Views: 337

Answers (3)

alinsoar
alinsoar

Reputation: 15793

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.

Upvotes: 0

Gilles Quénot
Gilles Quénot

Reputation: 185105

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

Upvotes: 2

RockWad
RockWad

Reputation: 15

Here's an example using AWK

cat /usr/share/nmap/nmap-services | awk '{print $2}' | awk -F\/ '{print $1}'

Upvotes: 0

Related Questions