Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配Bash中MMLS命令的第二列_Regex - Fatal编程技术网

Regex 正则表达式匹配Bash中MMLS命令的第二列

Regex 正则表达式匹配Bash中MMLS命令的第二列,regex,Regex,我正在编写一个脚本来匹配来自MMLS的特定值(起始列) 输出类似于: # Slot Start End Length Description #00: Meta 0000000000 0000000000 0000000001 Safety Table #01: ----- 0000000000 0000002047 0000002048 Unallocated #02: Meta 00000

我正在编写一个脚本来匹配来自MMLS的特定值(起始列)

输出类似于:

#     Slot    Start        End          Length       Description
#00:  Meta    0000000000   0000000000   0000000001   Safety Table
#01:  -----   0000000000   0000002047   0000002048   Unallocated
#02:  Meta    0000000001   0000000001   0000000001   GPT Header
#03:  Meta    0000000002   0000000033   0000000032   Partition Table
#04:  00      0000002048   0001026047   0001024000   SYSTEM
#05:  01      0001026048   0001288191   0000262144   Microsoft reserved partition
#06:  02      0001288192   0625141759   0623853568   Windows
#07:  -----   0625141760   0625142447   0000000688   Unallocated   
由于不熟悉正则表达式,我当前的正则表达式如下所示:

 [^ ]+\s+[^](?=\s.*SYSTEM)
哪一个匹配系统行的Slot、Start和End以及尾随空格,如果我只想匹配Start列,那么最好的方法是什么


约束条件:并非总是有7个插槽,大小会有所不同。

要匹配所有
start
列(#3):

要仅在行中找到系统时匹配,请执行以下操作:

/^(?:\S+\s+){2}(\S+)(?=\s.*SYSTEM)/gm


更新:如果在Ubuntu中运行,您可以使用如下简单的awk:

awk '/SYSTEM/{print $3}' file
0000002048

如果您只想使用
awk

要打印任何ith,您可以使用awk作为:

awk '{print $i}'
如果要选择包含
图案的特定行
,并打印其第i列:

在您的示例中:

要完全打印
start
列,请执行以下操作:

awk '{print $3}'
输出:

Start
0000000000
0000000000
0000000001
0000000002
0000002048
0001026048
0001288192
0625141760
0000002048
要打印包含
系统的行的
开始
列值

awk '/SYSTEM/{print $3}'
输出:

Start
0000000000
0000000000
0000000001
0000000002
0000002048
0001026048
0001288192
0625141760
0000002048

您想要第二列还是第一列?您运行的是哪种语言/工具?因为这实际上不需要regex..@AvinashRaj,我在Bash中匹配它的目的是使用描述限定符将开始值分配给一个变量以供以后使用。有没有更好的方法来解决这个问题?@ImNotLeet:你想使用Unix工具来解决这个问题吗?@anubhava它在一个无绑定的环境中