需要RegExp帮助Linux Bash grep命令来过滤掉包含方括号的行

时间:2022-05-23 01:54:16

Using the following example, I need to filter out the line containing 'ABC' only, while skipping the lines matching 'ABC' that contain square brackets:

使用以下示例,我需要过滤掉仅包含'ABC'的行,同时跳过与'ABC'匹配的包含方括号的行:

2012-04-04 04:13:48,760~sample1~ABC[TLE 5332.233 2/13/2032 3320392]:CAST
2012-04-04 04:13:48,761~sample2~ABC
2012-04-04 04:13:48,761~sample3~XYZ[BAC.CAD.ABC.CLONE 232511]:TEST

Here is what I have, but so far I'm unable to successfully filter out the lines with square brackets:

这是我所拥有的,但到目前为止,我无法使用方括号成功过滤掉这些行:

bash-3.00$ cat Metrics.log | grep -e '[^\[\]]' | grep -i 'ABC'

bash-3.00 $ cat Metrics.log | grep -e'[^ \ [\]]'| grep -i'ABC'

Please help?

请帮忙?

2 个解决方案

#1


3  

Edited based on comments:

根据评论编辑:

Try grep -i 'ABC' Metrics.log | grep -v "[[]" | grep -v "ABC\w"

试试grep -i'ABC'Metrics.log | grep -v“[[]”| grep -v“ABC \ w”

Input:

输入:

2012-04-04 04:13:48,760~sample1~ABC[TLE 5332.233 2/13/2032 3320392]:CAST
2012-04-04 04:13:48,761~sample2~ABC
2012-04-04 04:13:48,761~sample3~XYZ[BAC.CAD.ABC.CLONE 232511]:TEST
2012-04-04 04:13:48,761~sample4~XYZ
2012-04-04 04:13:48,761~sample5~ABCD
2012-04-04 04:13:48,761~sample6~ABC:TEST

Output:

输出:

2012-04-04 04:13:48,761~sample2~ABC
2012-04-04 04:13:48,761~sample6~ABC:TEST

#2


1  

 $cat log |  grep -v '\[.*\]' | grep ABC

#1


3  

Edited based on comments:

根据评论编辑:

Try grep -i 'ABC' Metrics.log | grep -v "[[]" | grep -v "ABC\w"

试试grep -i'ABC'Metrics.log | grep -v“[[]”| grep -v“ABC \ w”

Input:

输入:

2012-04-04 04:13:48,760~sample1~ABC[TLE 5332.233 2/13/2032 3320392]:CAST
2012-04-04 04:13:48,761~sample2~ABC
2012-04-04 04:13:48,761~sample3~XYZ[BAC.CAD.ABC.CLONE 232511]:TEST
2012-04-04 04:13:48,761~sample4~XYZ
2012-04-04 04:13:48,761~sample5~ABCD
2012-04-04 04:13:48,761~sample6~ABC:TEST

Output:

输出:

2012-04-04 04:13:48,761~sample2~ABC
2012-04-04 04:13:48,761~sample6~ABC:TEST

#2


1  

 $cat log |  grep -v '\[.*\]' | grep ABC