2

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.

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

1 Answer 1

2

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:]]\[.*\]$"

Output:

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

This can be made simpler:

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

Check:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .