It might depend on where you are using the regular expression. Each software might implement regular expressions a little different. Anyway, if I understand what you are trying to do, this might work:
$type_log(_$instance)?.log.$date.*
The ( ) defines a group, and the ? indicates the group is used 0 or 1 times, as you were trying to do.
Imagine i have several files with these forms :
exploitation_ora.log.2013-03-28.001
server.log.2013-03-28.tar.gz
After creating variables, i have this :
$type_$instance.log.$date.001
$type.log.$date.tar.gz
What's pattern matching notation or regular expression [????????] do i put to replace _$instance or nothing ?
The regular expression seems rather complex (In other words, I couldn't figure it out :)), so I tried a different way.
Instead of making one all-purpose, confusing pattern, I suggest to to let find use either of two patterns. See if this makes sense, and does the job:
$ cat temp.sh
touch exploitation_ora.log.2013-03-28.001
touch server.log.2013-03-28.tar.gz
date=2013-03-28
instance=ora
for type in exploitation server; do
echo Looking just for type = $type
find . \( -name "${type}_$instance.log.$date.*" -o \
-name "${type}.log.$date.*" \) -print
echo
done
echo Looking for type = exploitation OR server
find . \( -name "exploitation_$instance.log.$date.*" -o \
-name "server.log.$date.*" \) -print
$ ./temp.sh
Looking just for type = exploitation
./exploitation_ora.log.2013-03-28.001
Looking just for type = server
./server.log.2013-03-28.tar.gz
Looking for type = exploitation OR server
./exploitation_ora.log.2013-03-28.001
./server.log.2013-03-28.tar.gz
So the final find command would be the one to use, unless there is some other complication I'm missing.
Like Hanson44 notes, you need curly brackets around the shell variable, because the underscore can be part of the variable name. Also, the pattern needs to be quoted for use with -name . For a single pattern try this:
find /usr -name "${type}_log*.log.${date}.*"
-- [????????] is equivalent to [?] and \? and this means a single actual question mark.
I know it seems like it might work, but I don't think it works:
$ cat temp.sh
touch exploitation_ora.log.2013-03-28.001
touch server.log.2013-03-28.tar.gz
date=2013-03-28
instance=ora
for type in exploitation server; do
echo Looking just for type = $type
find . -name "${type}_log*.log.${date}.*" -print
done
$ ./temp.sh
Looking just for type = exploitation
Looking just for type = server
Might not it be better to let the poster test the previous solution that seems to work fine, instead of confusing the situation with other possible solutions? The thread will still be here April 3rd.
As almost usual, specifications / samples in posts #1 and #6 are contradictory if not mutually exclusive unless we assume a variable called type_log :
post #1:
@hanson44: As there's no _log in the filenames taken from post #6, that find won't work, of course. If you use the real file names, touch ed above, it will:
$ for type in exploitation server; do echo Looking just for type = $type; ls $type*.log.$date.*; done
Looking just for type = exploitation
exploitation_ora.log.2013-03-28.001
Looking just for type = server
server.log.2013-03-28.tar.gz