Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Extract Part of a String using Bash

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading