Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort semantic versions in bash?

Tags:

I would like to sort semantic versions (semver.org)

v1.4.0 v1.4.0-alpha v1.4.0-alpha1 v1.4.0-patch v1.4.0-patch9 v1.4.0-patch10 v1.4.0-patch2 v1.5.0 v1.5.0-alpha v1.5.0-alpha1 v1.5.0-alpha2 v1.5.0-patch v1.5.0-patch1 

in proper way. For instance, as version_compare() does in PHP (it doesn't directly, but can be used for that).

Of course, sort -V|--version-sort doesn't work here.

$ echo 1.0 1.0-alpha | tr ' ' "\n" | sort --version-sort 1.0 1.0-alpha 

Is there some exist approach?

P.S.

In common sense, it should follow this schema:

1.0.0-alpha    < 1.0.0-alpha.1      < 1.0.0-alpha.beta        < 1.0.0-beta         < 1.0.0-beta.2           < 1.0.0-beta.11            < 1.0.0-rc.1 < 1.0.0              < 1.0.0-patch < 1.0.0-patch.1. 

P.P.S.

Semver 2.0 doesn't support patches, but it's needed.

like image 455
Kirby Avatar asked Nov 02 '16 22:11

Kirby


People also ask

How do you sort semantic versioning?

If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.

How do I sort in reverse order in Linux?

-r Option: Sorting In Reverse Order: You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default. Example: The input file is the same as mentioned above.

Why is semantic versioning important?

Semantic versioning is a formal convention for determining the version number of new software releases. The standard helps software users to understand the severity of changes in each new distribution. A project that uses semantic versioning will advertise a Major, Minor and Patch number for each release.

Why is it called semantic versioning?

Semantic Versioning is a versioning scheme for using meaningful version numbers (that's why it is called Semantic Versioning). Specifically, the meaning revolves around how API versions compare in terms of backwards-compatibility.

How do I sort a list of semantic version strings?

Sorting an array of semantic versions. If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.

How do I sort a list of release versions by version?

If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.

How many types of sort options are there in Bash?

The sort command comes with 31 options (13 main and 18 categorized as other). Most experienced bash programming (even experts) know only a few main sort options required to get by. Others are seldom touched. Lucky for you we have time to touch them all.

What is semantic versioning?

“Semantic versioning” is a system of versioning software, whereby the version is defined by three numbers: Major release number, minor release number, and patch release number. These values come in string form as “MAJOR.MINOR.PATCH”, so “5.2.1” means the fifth major release, second minor release, first patch.


1 Answers

Well, we could trick sort -V by adding a dummy character at the end of the string for lines that do not contain a hyphen:

$ echo "$versions" | sed '/-/!{s/$/_/}' | sort -V | sed 's/_$//' v1.4.0-alpha v1.4.0-alpha1 v1.4.0-patch v1.4.0-patch2 v1.4.0-patch9 v1.4.0-patch10 v1.4.0 v1.5.0-alpha v1.5.0-alpha1 v1.5.0-alpha2 v1.5.0-patch v1.5.0-patch1 v1.5.0 

Underscore lexically sorts after hyphen. That's the trick.

like image 179
glenn jackman Avatar answered Sep 24 '22 05:09

glenn jackman