rdupz
rdupz

Reputation: 2313

Parse jar filenames

Let's say you want the best pattern to extract both name and version from a jar filename. I got something not so bad which works on regexr but not with bash. This is because bash does not support non-greedy regex. So how should one handle this?

#!/bin/bash

filename="./log4j-enhanced-3.5.100-v20130422-1538.jar"

if [[ "$filename" =~ \.\/(.*?)-([0-9].*)\.jar ]]; then
        echo "name    =  ${BASH_REMATCH[1]}"
        echo "version =  ${BASH_REMATCH[2]}"
fi

# expected :
# name    = log4j-enhanced
# version = 3.5.100-v20130422-1538

Usage of unix utilities is ok, but please make it readable.

Upvotes: 1

Views: 1187

Answers (3)

nwk
nwk

Reputation: 4050

If you want to accomplish the same thing without using Bash's special features, here is what you can do:

#!/bin/sh
filename="./log4j-enhanced-3.5.100-v20130422-1538.jar"

regex='.\/\(.*\)-\([0-9]\+\.[0-9]\+.*\)\.jar'

name="$(echo $filename | sed -e "s/$regex/\1/")"
version="$(echo $filename | sed -e "s/$regex/\2/")"

echo "   name: $name"
echo "version: $version"

Output:

   name: log4j-enhanced
version: 3.5.100-v20130422-1538

Upvotes: 0

Cyrus
Cyrus

Reputation: 88766

With GNU bash's Parameter Expansion :

filename="./log4j-enhanced-3.5.100-v20130422-1538.jar"

filename="${filename#./}"
name="${filename%%-[0-9]*}" 
version="${filename#$name-}"
version="${version%.jar}"

echo "name    = $name"
echo "version = $version"

Output:

name    = log4j-enhanced
version = 3.5.100-v20130422-1538

Upvotes: 5

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use

filename="./log4j-enhanced-3.5.100-v20130422-1538.jar"

if [[ "$filename" =~ [.]/([[:alnum:]-]*)-([0-9].*)[.]jar ]]; then
        echo "name    =  ${BASH_REMATCH[1]}"
        echo "version =  ${BASH_REMATCH[2]}"
fi

See the Ideone demo

The [[:alnum:]-]* will greedily match alphanumeric and hyphen characters up to a hyphen followed with a digit.

Upvotes: 2

Related Questions