grep with "[" and "]" and "dot" within the search string

Hello.
Following recommendations for one of my threads, this is working perfectly :

#!/bin/bash
CNT=$(  grep -c -e  "some text 1"   -e  "some text 2"  -e  "some text 3"    "/tmp/log_file.txt" ) 

Now I need a grep success for some thing like :

#!/bin/bash
CNT=$(  grep -c -e  "some text_1 REGEX_PATERN some other text"   -e  "some text 2"  -e  "some text 3"    "/tmp/log_file.txt" ) 

When the log file /tmp/log_file.txt contains either
some text_1 [done] some other text
or
some text_1 [.done] some other text
or
some text_1 [..done] some other text
or
some text_1 [...done] some other text
and so on.

Many Thanks

If your pattern is literal string and not a regex, use fgrep instead of grep .

If i get it correctly you search for "[", then a variable numbers of dots, then "done]", is that correct?

If so, your grep-regexp could look like:

grep '\[\.*done\]'

You can use any special character in regexps by "escaping" them. That is: put a backslash in front of them. "." means any character, while "\." means a literal dot. The same goes for "[", etc..

I hope this helps.

bakunin

1 Like

Exactly.
And that do the job
Thank you for helping.

THis thread is closed.

1 Like

"fgrep" is a faster variant of "grep". It uses simplified regular expressions, but this speeds up its performance considerably. For small files this will make no big difference but with large files it can save a lot of time. On the other hand "fgrep" is not as versatile as "grep", so there indeed are cases where "grep" will do the job but "fgrep" won't.

As a general rule: use "fgrep" when you can and "grep" when you must.

Corollary: the same goes for "grep" and "egrep" - at least where they are different. "egrep" can do even more than "grep", but again at the expense of speed. Its operation is even slower than that of "grep". In some systems, though, one is just a hard link to the other because the makers saved some time and effort in silently merging these two tools.

I hope this helps.

bakunin