1

I am trying to automate the deployment of war files that are generated by our build server using maven. I need to be able to extract the project name from the war filename so I can rename the file before deploying it. (Because tomcat uses the filename as the default context path and I don't want the version to be in there)

The filenames are usually in the form projectname-version. e.g:

myproject-1.2.3-SNAPSHOT.war
my-better-project-1.2.3-RELEASE.war
my-fancy-project-1.2.3.war
  • SNAPSHOT or RELEASE may or may not be included in the filename.
  • One tricky part is to extract the project name when the project name includes dashes.

The result should be the project name without the version information:

myproject
my-better-project
my-fancy-project

How can I achieve this using bash and sed (or only bash)?

2

With GNU sed:

sed 's/-[0-9]\+.*//' file

Output:

myproject
my-better-project
my-fancy-project

With bash:

while read -r string; do
  [[ $string =~ (-[0-9]+.*) ]] && echo "${string%${BASH_REMATCH[1]*}}"
done < file

With GNU grep:

grep -oP '.*(?=-[0-9]+.*)' file
  • Thank you very much. I am using the sed solution for now: CONTEXT_PATH=echo $1 | sed 's/-[0-9]\+.*//' – Daniele Torino Jul 2 '15 at 8:48
  • 1
    or CONTEXT_PATH=$(sed 's/-[0-9]\+.*//' <<< "$1") – Cyrus Jul 2 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.