0

我在一个目录中有多个文件。我想提取包含整数值大于 45 的所有文件中的每一行。

目前,我正在使用:

grep "IO resumed after" *

它向我显示了此字符串“IO 恢复后”的所有文件

4

3 回答 3

6

最好awk用于此:

awk 'match($0,"IO resumed after") { if (substr($0,RSTART+RLENGTH)+0 > 45) print }' file

这将搜索字符串“IO resumed after”,如果找到该字符串,它将获取该字符串之后的所有内容并将其转换为数字:如果“IO resumed after”之后的子字符串以数字开头,那么它将转换为当我们只是将零添加到它时,这个数字。

这仅在该行如下所示时才有效:

xxxxIO resumed after_nnnnyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

其中xy是随机字符,下划线是任何空白序列,n是数字。

您可以使用以下命令集对其进行测试:

$ seq 40 0.5 50 | awk '{print "foo IO resumed after",$0,"random stuff"}' \
  | awk 'match($0,"IO resumed after") { if (substr($0,RSTART+RLENGTH)+0 > 45) print }'

输出:

foo IO resumed after 45.5 random stuff
foo IO resumed after 46.0 random stuff
foo IO resumed after 46.5 random stuff
foo IO resumed after 47.0 random stuff
foo IO resumed after 47.5 random stuff
foo IO resumed after 48.0 random stuff
foo IO resumed after 48.5 random stuff
foo IO resumed after 49.0 random stuff
foo IO resumed after 49.5 random stuff
foo IO resumed after 50.0 random stuff
于 2020-08-07T09:01:03.967 回答
0

您可以使用替代项和重复计数来定义大于 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,\}\)'
于 2020-08-07T09:36:20.777 回答
0

看起来我需要学习awk,直到那时我有一个 bash 解决方案。如果秒没有小数点,那么这个:

while read line; do
    number=${line//*after}
    number=${number//seconds*}
    ((number>45)) && echo $line
done <<< $(grep "IO resumed after" *)

否则我们必须使用bc

while read line; do
    number=${line//*after}
    number=${number//seconds*}
    case $(bc <<< "$number>45") in 1) echo "$line";; esac
done <<< $(grep "IO resumed after" *)
于 2020-08-07T12:19:36.033 回答