grep一个字符串后跟一系列数字

时间:2014-09-24 10:50:58

标签: regex grep

我有这个日志:

2014-09-24 10:07:44 +0000 severity=INFO, Completed 200 OK in 955ms (Views: 566.4ms | ActiveRecord: 246.9ms)
2014-09-24 10:06:53 +0000 severity=INFO, Completed 404 Not Found in 13ms (Views: 12.0ms | ActiveRecord: 0.0ms)
2014-09-24 10:06:43 +0000 severity=INFO, Completed 500 OK in 939ms (Views: 547.8ms | ActiveRecord: 253.0ms)
2014-09-24 10:05:44 +0000 severity=INFO, Completed 501 OK in 721ms (Views: 495.1ms | ActiveRecord: 198.2ms)
2014-09-24 10:04:43 +0000 severity=INFO, Completed 200 OK in 997ms (Views: 592.6ms | ActiveRecord: 238.4ms)
2014-09-24 10:03:43 +0000 severity=INFO, Completed 401 OK in 983ms (Views: 584.8ms | ActiveRecord: 237.0ms)

我想用正则表达式执行grep以获取包含不同类型错误的所有行。 例如:

所有客户端错误:已完成4 *

2014-09-24 10:06:53 +0000 severity=INFO, Completed 404 Not Found in 13ms (Views: 12.0ms | ActiveRecord: 0.0ms)
2014-09-24 10:03:43 +0000 severity=INFO, Completed 401 OK in 983ms (Views: 584.8ms | ActiveRecord: 237.0ms)

所有服务器错误:已完成5 *

2014-09-24 10:06:43 +0000 severity=INFO, Completed 500 OK in 939ms (Views: 547.8ms | ActiveRecord: 253.0ms)
2014-09-24 10:05:44 +0000 severity=INFO, Completed 501 OK in 721ms (Views: 495.1ms | ActiveRecord: 198.2ms)

你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式进行搜索:

grep 'Completed [0-9]' error.log

或者只获取以45开头的状态代码:

grep 'Completed [45]' error.log