Solved

Expr Match: Why doesn't this expresssion work?

Posted on 2014-04-24
5
276 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 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
Gerwin Jansen 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now