fdisk -l | 带通配符的 grep 命令不显示结果

fdisk -l | 带通配符的 grep 命令不显示结果

为什么使用命令

fdisk -l | grep sd? 
fdisk -l | grep sd?? 

在这种情况下不要工作,

fdisk -l | grep sd[ab] 

有用吗?

终端窗口

答案1

并非所有的正则表达式grep都与 相同shell。要列出磁盘,请使用,使用以下

fdisk -l | grep 'sd?*'

事实上,上述内容可以通过以下简化版本实现:

fdisk -l | grep sd

答案2

你可能想要使用egrep

fdisk -l | grep sd..?

但使用正确的正则表达式就足够了:

fdisk -l | grep 'sd.\|sd..'

解释:

egrep相当于grep -E采用不同的增强版本的常用表达

.方法任意字符

\|方法或者

参见 man grep:

-E, --extended-regexp
       Interpret PATTERN as an extended regular  expression  (ERE,  see below).

   grep understands three different versions of regular expression syntax:
   “basic”  (BRE), “extended” (ERE) and “perl” (PCRE).  In GNU grep, there
   is no difference in available functionality between basic and  extended
   syntaxes.  In other implementations, basic regular expressions are less
   powerful.   The  following  description  applies  to  extended  regular
   expressions;  differences  for basic regular expressions are summarized
   afterwards.   Perl-compatible  regular  expressions   give   additional
   functionality,  and are documented in pcresyntax(3) and pcrepattern(3),
   but work only if PCRE is available in the system.

   The period . matches any single character.

Alternation
   Two regular expressions may be joined by the infix operator |; the resulting regular expression matches any string matching either alternate expression.

相关内容