Pattern matching notation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern matching notation
# 1  
Old 07-29-2013
Pattern matching notation

Hello

I have two kinds of logs like

Code:
server.log

Code:
server.log.2013-07-27.001

i want to create a variable which look like this (with a pipe)
Code:
log_name=server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])

But i tried many cases but it didn't work.
Is it possible ? If yes, can you help me.
# 2  
Old 07-29-2013
Have you tried putting it in double quotes...
Code:
log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

# 3  
Old 07-29-2013
Quote:
Originally Posted by amazigh42
Hello

I have two kinds of logs like

Code:
server.log

Code:
server.log.2013-07-27.001

i want to create a variable which look like this (with a pipe)
Code:
log_name=server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])

But i tried many cases but it didn't work.
Is it possible ? If yes, can you help me.
What are you planning to do with this variable?
What is the value of the variable YYYY when you try setting log_name?

If you have a variable YYYY that is set to the current year, don't you also need $MM and $DD so they can expand to the current month and day, respectively?

Do you want $YYYY to be expanded when log_name is set, or do you want $log_name to contain the literal string $YYYY?

If you want $YYYY (and any other variables you may define) to be expanded when you set log_name, use:
Code:
log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

as shamrock suggested.

If you want the literal string $YYYY use:
Code:
log_name='server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])'

# 4  
Old 07-30-2013
Thank you for your help.
I'm going to explain my aim

In my directory, it may be that there is this
Code:
ll
-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7753380 Jul 28 23:59 server.log.2013-07-28.001
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005
-rw-r--r--  1 xxxx xxxxx 10491424 Jul 30 10:09 server.log.2013-07-30.002
-rw-r--r--  1 xxxx xxxxx 10491929 Jul 30 12:05 server.log.2013-07-30.003
-rw-r--r--  1 xxxx xxxxx 10489096 Jul 30 14:15 server.log.2013-07-30.004
 server.log.2013-07-30.004

i create this variables
Code:
AAAA_MM_JJ=2013-07-29

Code:
log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

if i launch
Code:
ll $log_name

I would like to obtain :
Code:
-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005

But it didn't work, i obtain that.

Code:
ll $log_name
/bin/ls: server.(log|log.2013-07-29.[0-9][0-9][0-9]): No such file or directory

# 5  
Old 07-30-2013
So you want:
Code:
log_name="server.log server.log.$AAAA_MM_JJ.[0-9][0-9][0-9]"

in your script sometime after you have set the variable AAAA_MM_JJ.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-30-2013
Quote:
Originally Posted by Don Cragun
So you want:
Code:
log_name="server.log server.log.$AAAA_MM_JJ.[0-9][0-9][0-9]"

in your script sometime after you have set the variable AAAA_MM_JJ.
Thanks a lot Smilie it works with a space.
Code:
ll $log_name

I' obtain now :
Code:
-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005

Do you know why doesn't it work with the command FIND ?
Code:
find . -type f -iname "$log_name" 2> /dev/null

# 7  
Old 07-30-2013
Quote:
Originally Posted by amazigh42
Thanks a lot Smilie it works with a space.
Code:
ll $log_name

I' obtain now :
Code:
-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005

Do you know why doesn't it work with the command FIND ?
Code:
find . -type f -iname "$log_name" 2> /dev/null

Of course I know why. And I think you do, too.
Note that the command you used with ll wasl
Code:
ll $log_name

(with no quotes). So ls was given two operands: server.log and server.log.2013-07-29.[0-9][0-9][0-9].

With find, you added the quotes because a -iname primary takes a single pattern argument; not two. But, by putting quotes around the two patterns, you created a single pattern that will only match filenames that start with server.log server.log.2013-07-29. and end with three decimal digits (and you don't have any files in this directory with names starting with server.log followed by a space).

For this application you need a different approach:
Code:
log_name1="server.log"
log_name2="$log_name1.$$AAAA_MM_JJ.[0-9][0-9][0-9]"
find . -type f \( -iname "$log_name1" -o -iname "$log_name2" \) 2> /dev/null

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Perl: scientific notation to decimal notation

hello folks, I have few values in a log which are in scientific notation. I am trying to convert into actual decimal format or integer but couldn't able to convert. Values in scientific notation: 1.1662986666666665E-4 2.0946799999999998E-4 3.0741333333333333E-6 5.599999999999999E-7... (2 Replies)
Discussion started by: scriptscript
2 Replies

5. Shell Programming and Scripting

Pattern matching notation

Hello, I want to simplify two commands into one. 1st command $type_log_$instance.log.$date.0012nd command $type_log.log.$date.tar.gzInto blue brackets, How do I do to replace the pattern by a blank or _$instance ? $type_log?_$instance].log.$date.*Thank you. (13 Replies)
Discussion started by: amazigh42
13 Replies

6. Shell Programming and Scripting

Pattern matching notation

Hello, I want to match this patterns IS01ORA IS02ORA ... ... IS08ORA With that : ISORA But it doesn't work, can you correct it ? Thanks (8 Replies)
Discussion started by: amazigh42
8 Replies

7. Shell Programming and Scripting

[DATE] Pattern matching notation

Hello, I want to verify the format date like 2013-03-08 (YYYY-MM-DD) It doesn't work because the pattern matching notation below returns false while the date is right. Can you help me ? Thanks in advance case "$6" in (-0-0 | -1-1 | -1-2 | -1-3) # Nothing, OK ! ;; (*) echo 'Fatal,... (4 Replies)
Discussion started by: amazigh42
4 Replies

8. Shell Programming and Scripting

Convert decimal notation to ANSI point code notation

wondering if anyone has any thoughts to convert the below thru a shell script Convert decimal signalling point notation to ANSI point code notation There is a site that does that conversion but i need to implement the solution in a shell script.....Thoughts.... OS: Solaris 9 ... (4 Replies)
Discussion started by: aavam
4 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question