Litox
Litox

Reputation: 163

Substring from a file

I need to extract coincidence lines from a file with a regular expression:

This is the content of file:

netbios-ns      137/tcp                         # NETBIOS Name Service
netbios-ns      137/udp
hkp             11371/tcp                       # OpenPGP HTTP Keyserver
hkp             11371/udp                       # OpenPGP HTTP Keyserver
bprd            13720/tcp                       # VERITAS NetBackup
bprd            13720/udp
vopied          13783/udp

I need to filter using 137 with grep:

grep -n -e "137" file

The output must be:

netbios-ns      137/tcp                         # NETBIOS Name Service
netbios-ns      137/udp

Upvotes: 0

Views: 170

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

Use grep's word boundaries: grep '\<137\>' file

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85765

If you always have preceding whitespace and a trailing slash then:

$ grep " 137/" file
netbios-ns 137/tcp # NETBIOS Name Service
netbios-ns 137/udp

Or more robust, check for non-digits either side:

$ grep "[^[:digit:]]137[^[:digit:]]" file
netbios-ns 137/tcp # NETBIOS Name Service
netbios-ns 137/udp

Upvotes: 1

Related Questions