?
Solved

Expr Match: Why doesn't this expresssion work?

Posted on 2014-04-24
5
Medium Priority
?
304 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 1600 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 1600 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 400 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 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