Solved

Expr Match: Why doesn't this expresssion work?

Posted on 2014-04-24
5
280 Views
Last Modified: 2014-04-24
Background:
Very new to linux and shell scripting. I'm trying to extract the year, month and day numbers from a directory string like this:

      /path/to/dir/yyyy/mm/dd

I started by trying to extract the 4 digit year with something like this:

      expr match "/path/to/dir/2014/02/17" '.*/\([[:digit:]]{4}\)/.*/.*'

But it just returned an empty string.  Yet, when I used "*" instead of "{4}"  

      expr match "/path/to/dir/2014/02/17" '.*/\([[:digit:]]{4}\)/.*/.*'

It returned the 4 digit year:

    2014

Can anyone explain why?  Also, any better ways to extract the date parts:  yyyy , mm, and dd?
0
Comment
Question by:_agx_
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 400 total points
ID: 40020738
>> Also, any better ways to extract the date parts:  yyyy , mm, and dd?
Possibly, does the string always end in the yyyy/mm/dd format? If so then it could be easier, using 'rev' and 'cut' for example:

# string="/path/to/dir/2014/02/17"
# echo ${string}
/path/to/dir/2014/02/17

# echo ${string} | rev | cut -d"/" -f1 | rev
17

# echo ${string} | rev | cut -d"/" -f2 | rev
02

# echo ${string} | rev | cut -d"/" -f3 | rev
2014

And you can assign output to a variable like this:

year=$(echo ${string} | rev | cut -d"/" -f3 | rev)
0
 
LVL 52

Author Comment

by:_agx_
ID: 40020835
> Possibly, does the string always end in the yyyy/mm/dd format?

Yep. Nice tip. That works perfectly.

Any idea why my original syntax didn't work with "{4}"?  If it's a syntax error on my part, I'd like to understand what it is ... so I don't do it again :)
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 400 total points
ID: 40021062
It's about escaping the special characters, this will work:

expr match "/path/to/dir/2014/02/17" '.*\([[:digit:]]\{4\}\)/.*/.*'
-> 2014 matches

expr match "/path/to/dir/2014/02/17" '.*\([[:digit:]]\{2\}\)/.*/.*'
-> 14 matches

The pattern between \( ... \) matches, because of the /.*/.* at the end

If you change like this:

expr match "/path/to/dir/2014/02/17" '.*\([[:digit:]]\{2\}\)/.*'
-> 02 matches
0
 
LVL 2

Assisted Solution

by:Jack Frost
Jack Frost earned 100 total points
ID: 40021076
Even though you have the pattern in quotes the shell sees the curly brackets and tries to preprocess them.  They just need to be escaped with a backslash:

j@jf-linux:~$ expr match "/path/to/dir/2014/02/17" '.*/\([[:digit:]]\{4\}\)/.*/.*'
2014
0
 
LVL 52

Author Comment

by:_agx_
ID: 40021115
Ahh. That makes total sense. Thanks for solving the mystery.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Anything Suspicious in this Report Email on Linux Server 4 47
video edge NVR Device Discovery Problem 4 45
Run same command on multiple files in Linux 3 32
linux SFTP 8 44
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question