简体   繁体   中英

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

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

使用grep的单词边界: grep '\\<137\\>' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM