I want to match all the lines that are of the below pattern in a file using grep.

Abc::xyz() [18-Feb-15 12:09:16]

I came up with below regex with grep , but it is not returning match. Please help with the correct regex.

grep -E "^[:alnum:]::[:alnum:]\(\)[:space:]\[.*\]$" <file_name>

In python, below regex works , but not able to use the same with grep.

^.*::.*\(\) \[.*\]$

If you want to stick with you one, use this:

grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$"

For example:

$ echo "Abc::xyz() [18-Feb-15 12:09:16]" | \
    grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$"


Abc::xyz() [18-Feb-15 12:09:16]

This can be made simpler:

grep -E "^[^:]+::[^(]+\(\) \[[^]]+\]$"


$ echo "Abc::xyz() [18-Feb-15 12:09:16]" | grep -E "^[^:]+::[^(]+\(\) \[[^]]+\]$"
Abc::xyz() [18-Feb-15 12:09:16]
  • not matching anything – g4ur4v Apr 17 '15 at 23:23
  • @g4ur4v: working in my case..check my edits – heemayl Apr 17 '15 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.