您可以使用替代和重复计数来定义大于 45 的数字的搜索模式。
此解决方案假定数字是不带小数点的整数。
grep 'IO resumed after \(4[6-9]\|[5-9][0-9]\|[0-9]\{3,\}\) seconds'
或者更短的egrep:
egrep 'IO resumed after (4[6-9]|[5-9][0-9]|[0-9]{3,}) seconds'
我用
测试了这个模式
for i in 1 10 30 44 45 46 47 48 49 50 51 60 99 100 1234567
do
echo "foo IO resumed after $i seconds bar"
done | grep 'IO resumed after \(4[6-9]\|[5-9][0-9]\|[0-9]\{3,\}\) seconds'
打印出来的
foo IO resumed after 46 seconds bar
foo IO resumed after 47 seconds bar
foo IO resumed after 48 seconds bar
foo IO resumed after 49 seconds bar
foo IO resumed after 50 seconds bar
foo IO resumed after 51 seconds bar
foo IO resumed after 60 seconds bar
foo IO resumed after 99 seconds bar
foo IO resumed after 100 seconds bar
foo IO resumed after 1234567 seconds bar
如果数字(可以)有小数点,则很难为数字 > 45 定义模式,例如45.1.
此模式允许小数点或逗号后跟数字并实现条件 >= 46。
grep 'IO resumed after \(4[6-9]\|[5-9][0-9]\|[0-9]\{3,\}\)\([.,][0-9]*\)\{,1\} seconds'
第二次编辑:
上面的模式不处理可能的前导零。正如用户kvantour 在评论中所建议的那样,可以扩展该模式以处理此问题。此外,如果不需要检查seconds 部分,则可以省略小数的模式。
数字 >= 45 的模式,带有可选的前导零:
grep 'IO resumed after 0*\(4[5-9]\|[5-9][0-9]\|[1-9][0-9]\{2,\}\)'