Need RegExp help for Linux Bash grep command to filter out lines containing square brackets
-
29-05-2021 - |
Question
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?
Solution
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
OTHER TIPS
$cat log | grep -v '\[.*\]' | grep ABC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow