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

Return the last characters of a link in bash

I have the following link from my local deployment:

https://localhost/internal-repo/test-tools/local-repo/docker-image-internal-1.2-dirty.tgz

I want to return only 1.2-dirty, I started with:

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

less version.txt | awk 'NR==1 {print $1}'

but it returns the whole link.

Do you have any idea how I can proceed?

>Solution :

You can something like this:

awk -F'[-/]' '{gsub(/\.[^.]+$/, "", $NF); print $(NF-1) "-" $NF}'

-F'[-/]' sets the field separator to either ‘-‘ or ‘/’. This means that each part of the URL between ‘-‘ and ‘/’ will be treated as a separate field.

gsub(/\.[^.]+$/, "", $NF) uses the gsub function in AWK, which stands for "global substitution". It searches for a pattern in the specified field ($NF, which refers to the last field), and replaces it with the given string (in this case, an empty string)

print $(NF-1) "-" $NF prints the second-to-last field ($(NF-1)) followed by a hyphen and then the last field ($NF). This concatenation gives you the desired version string

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