Solved

Expr Match: Why doesn't this expresssion work?

Posted on 2014-04-24
5
285 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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