Lidia
Lidia

Reputation: 2123

Return substring before dash followed by a digit in bash

I have a string that can be one of:

1.) AA_BB-CC_xxxx-xx.y.y-xxxxxxxx-yyyyyy.tar.gz

or with prefix dropped:

2.) CC_xxxx-xx.y.y-xxxxxxxx-yyyyyy.tar.gz

where A,B,C,D are any number of letters and x and y are digits. I need to extract the following from the above:

AA_BB-CC_xxxx
CC_xxxx

Example:

standalone_version-WIN_2012-16.3.2-20180627-131137.tar.gz
WIN_2008-16.3.2-20180614-094525.tar.gz

need to extract:

standalone_version-WIN_2012
WIN-2008

I'm trying to discard everything from the end till the first dash followed by a digit is encountered. I'm using the following but it returns the whole string:

name=${image_file%%-[0-9].*}

Upvotes: 2

Views: 136

Answers (1)

cxw
cxw

Reputation: 17051

You were almost there! Instead of

name=${image_file%%-[0-9].*}

omit the dot:

name=${image_file%%-[0-9]*}

The expressions in bash %% string trims are patterns, not regular expressions. Therefore * alone matches any number of characters, not .* as in a regex.

Example (tested in bash 4.4.12(3)-release):

$ foo='standalone_version-WIN_2012-16.3.2-20180627-131137.tar.gz'
$ bar='WIN_2008-16.3.2-20180614-094525.tar.gz'
$ echo ${foo%%-[0-9].*}
standalone_version-WIN_2012-16.3.2-20180627-131137.tar.gz
    # oops
$ echo ${foo%%-[0-9]*}
standalone_version-WIN_2012
    # no dot - works fine
$ echo ${bar%%-[0-9]*}
WIN_2008
    # same here.

Upvotes: 4

Related Questions