Home > Software engineering >  How to Extract Part of a String using Bash
How to Extract Part of a String using Bash

Time:08-15

I have been trying to extract part of string in bash. I'm using it on Windows 10. Basically I want to remove "artifacts/" sfscripts_artifact_ and ".zip"

Original String

artifacts/online-order-api_sfscripts_artifact_1.5.6-6.zip

I've tried

input="artifacts/online-order-api_sfscripts_artifact_1.5.6-6.zip"
echo "${input//[^0-9.-]/}"

Output

--1.5.6-6.

Expected Output

online-order-api 1.5.6-6

CodePudding user response:

You may use this awk solution:

s='artifacts/online-order-api_sfscripts_artifact_1.5.6-6.zip'
awk -F_ '{gsub(/^[^\/]*\/|\.[^.]*$/, ""); print $1, $NF}' <<< "$s"

online-order-api 1.5.6-6

Or else this sed solution:

sed -E 's~^[^/]*/|\.[^.] $~~g; s~(_[^_] ){2}_~ ~;' <<< "$s"

online-order-api 1.5.6-6
  • Related