Solved

Expr Match: Why doesn't this expresssion work?

Posted on 2014-04-24
5
283 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 38

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 38

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

685 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