Pattern matching notation

Hello,

I want to simplify two commands into one.
1st command

$type_log_$instance.log.$date.001

2nd command

$type_log.log.$date.tar.gz

Into blue brackets, How do I do to replace the pattern by a blank or _$instance ?

$type_log[?_$instance].log.$date.*

Thank you.

$ echo '$type_log.log.$date.001' | awk -F"." -v str='_$instance' -v OFS="." ' {$1=$1 str} 1 '
$type_log_$instance.log.$date.001

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.

I may be poorly explained my problem.

$type[?????].log.$date.*

What should I put in place ????? equivalent to these two proposals below.

$type_$instance.log.$date.001
$type.log.$date.tar.gz

Why do you use the [ ] around the ????? section?

The [ ] is normally used for [A-Z0-9] etc., to represent a single character, which you probably know.

It would help to see some actual code. Otherwise, my best guess is the previous answer I gave.

Hello,
Thanks for your help.

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 ?

$type_$instance.log.$date.001
$type          .log.$date.tar.gz
$type[????????].log.$date.*

For example, i can include this in a script.

find /usr -name $type[????????].log.$date.*

Have a nice day

Thanks for explaining more.

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.

Thanks, I'll test it later, i'm on holiday, i 'll come back at april 3.

Have a nice week-end.

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.

As we do not know what to discriminate against, i.e. what to exclude, I think a simple * would do:
$type*.log.$date.*

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. :slight_smile:

@hanson44: I used the OP's specification in the first post and then that should be

touch exploitation_log_ora.log.2013-03-28.001
touch server_log.log.2013-03-28.tar.gz

I am not trying to confuse matters but the OP did ask for a unified pattern.

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:

$type_log_$instance.log.$date.001
$type_log.log.$date.tar.gz

post #6:

exploitation_ora.log.2013-03-28.001
server.log.2013-03-28.tar.gz

@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

Thanks a lot, it's working.
This site should be reimbursed by social insurance. :smiley: