7

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

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'

Please help?

2 Answers2

3

Edited based on comments:

Try 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
GetSet
  • 534
  • 2
  • 6
  • 1
    If it works for you, would you mind marking the answer as correct? Thanks! – GetSet Apr 05 '12 at 14:18
  • note, no need to cat Metrics.log, simply supply that as the input to the 1st `grep. grep -i 'ABC' Metrics.log | grep -v "[[]"` – matchew Apr 05 '12 at 14:32
  • Thanks. Marked as answered. Noted about the cat. One more case: need to skip false matches, like ABCD, but not ABC:TEST. – Alexander Len Apr 05 '12 at 15:27
  • Thanks. Last suggestion did not work for me in all cases, such as DABC, which I did not specify. I found this to work the best: `grep -i '\bABC\b'` – Alexander Len Apr 05 '12 at 16:01
1
 $cat log |  grep -v '\[.*\]' | grep ABC
Aftnix
  • 4,461
  • 6
  • 26
  • 43