简体   繁体   English

文件的子字符串

[英]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 : 我需要使用137grep进行过滤:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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