grep一个字符串,然后匹配两个或三个字符

时间:2021-04-21 19:18:47

Given an input file:

给定一个输入文件:

exception Error 1
exception Error 2
warning Error 123 (ignore)
warning Error 123 (ignore)
exception Error 3

I want to grep only lines which end with only Error 1 or Error 2, but not those which end with ignore. I don't want to use two grep commands by piping the output; I want to use only single grep command.

我只想grep只以Error 1或Error 2结尾的行,而不是那些以ignore结尾的行。我不想通过管道输出使用两个grep命令;我想只使用单个grep命令。

I tried something like grep "Error.." file though I am restricting it to two chars it still outputs ignore strings.

我尝试了类似grep“Error ..”文件的东西,虽然我将它限制为两个字符,它仍然输出忽略字符串。

1 个解决方案

#1


1  

You can use end anchor in regex:

你可以在正则表达式中使用结束锚:

grep -E 'Error [12]$' file
exception Error 1
exception Error 2

or word boundary:

或字边界:

grep -E 'Error [12]\b' file
exception Error 1
exception Error 2

#1


1  

You can use end anchor in regex:

你可以在正则表达式中使用结束锚:

grep -E 'Error [12]$' file
exception Error 1
exception Error 2

or word boundary:

或字边界:

grep -E 'Error [12]\b' file
exception Error 1
exception Error 2