正确地将输入输入到grep并对其执行正则表达式以获取数字和匹配的字符串

时间:2017-05-07 10:48:28

标签: regex grep

我有一个输出以下输出的脚本:

  

总体状态 - 确定(采样周期9秒) - 物理驱动器名称=" 0   C:" (好) - _PercentIdleTime = 100%,_ PercentBusyTime = 0%,   _PercentDiskTime = 1%,_ PercentDiskReadTime = 0%,_ PercentDiskWriteTime = 1%,_ DiskReadBytesPersec = 0.000B / sec,_DiskReadsPersec = 0.000 / sec,_DiskWriteBytesPersec = 0.00MB / sec,_DiskWritesPersec = 8.000 / sec,CurrentDiskQueueLength = 0,_AvgDiskQueueLength = 0.0,_AvgDiskReadQueueLength = 0.0,_AvgDiskWriteQueueLength = 0.0 |' _PercentIdleTime0 C:' = 100; ' _PercentBusyTime0 C:' = 0; ' _PercentDiskTime0 C:' = 1;   ' _PercentDiskReadTime0 C:' = 0; ' _PercentDiskWriteTime0 C:' = 1;   ' _DiskReadBytesPersec0 C:' = 0; ' _DiskReadsPersec0 C:' = 0;   ' _DiskWriteBytesPersec0 C:' = 71917; ' _DiskWritesPersec0 C:' = 8;   ' CurrentDiskQueueLength0 C:' = 0; ' _AvgDiskQueueLength0 C:' = 0.0;   ' _AvgDiskReadQueueLength0 C:' = 0.0; ' _AvgDiskWriteQueueLength0 C:' = 0.0;

使用grep我想过滤除 _DiskWriteBytesPersec = 0.00MB

之外的所有内容

我在http://regexr.com/上使用了以下正则表达式来成功完成此操作: 的(_ DiskWriteBytesPersec =(\ d *)?(\ d *)MB)

但是我在使用grep时遇到了困难。

1 个答案:

答案 0 :(得分:0)

正则表达式的问题是(_DiskWriteBytesPersec=(\d*)?.(\d*)MB),它不支持\d而是使用[0-9],您可以试试这个,它会正常工作。

echo "Overall Status - OK (Sample Period 9 sec) - Physical Drive Name="0 C:" (OK) - _PercentIdleTime=100%, _PercentBusyTime=0%, _PercentDiskTime=1%, _PercentDiskReadTime=0%, _PercentDiskWriteTime=1%, _DiskReadBytesPersec=0.000B/sec, _DiskReadsPersec=0.000/sec, _DiskWriteBytesPersec=0.00MB/sec, _DiskWritesPersec=8.000/sec, CurrentDiskQueueLength=0, _AvgDiskQueueLength=0.0, _AvgDiskReadQueueLength=0.0, _AvgDiskWriteQueueLength=0.0|'_PercentIdleTime0 C:'=100; '_PercentBusyTime0 C:'=0; '_PercentDiskTime0 C:'=1; '_PercentDiskReadTime0 C:'=0; '_PercentDiskWriteTime0 C:'=1; '_DiskReadBytesPersec0 C:'=0; '_DiskReadsPersec0 C:'=0; '_DiskWriteBytesPersec0 C:'=71917; '_DiskWritesPersec0 C:'=8; 'CurrentDiskQueueLength0 C:'=0; '_AvgDiskQueueLength0 C:'=0.0; '_AvgDiskReadQueueLength0 C:'=0.0; '_AvgDiskWriteQueueLength0 C:'=0.0;" | grep -Eo "_DiskWriteBytesPersec=[0-9]*?.[0-9]+MB";

使用grep -Eo "_DiskWriteBytesPersec=[0-9]*?.[0-9]+MB"

管道输出

命令输出: _DiskWriteBytesPersec=0.00MB

  

1。 E用于扩展正则表达式。

     

2。 O仅输出匹配的字符串。