bash - Remove duplicate package dependencies, sort by version -
i have file example:
"grunt": "0.4.5", "grunt": "1.0.1" "grunt": "1.0.1", "grunt-angular-templates": "0.5.7", "grunt-cli": "^0.1.13", "grunt-contrib-clean": "0.6.0", "grunt-contrib-compress": "0.12.0", "grunt-contrib-concat": "1.0.1", now want remove lines have duplicate prefixes keep ones have more recent versions. line starts grunt want keep 1 has version 1.0.1 remove other ones.
is there straightforward this?
one implementation naive approach simple:
sort -k1,1 -k2,2vr file | sort -k1,1 -u i.e: sort first field (package name) ascending, , second field (version) descending using -v/--version-sort (natural sort version numbers). in second pass (second sort invocation, -u/--unique flag) compare package name , drop duplicates (packages same name smaller version number, since after first pass greater versions appear @ top).
the result sample input is:
"grunt": "1.0.1", "grunt-angular-templates": "0.5.7", "grunt-cli": "^0.1.13", "grunt-contrib-clean": "0.6.0", "grunt-contrib-compress": "0.12.0", "grunt-contrib-concat": "1.0.1", however, since npm (and i'm assuming lines package.json) uses semantic versioning (semver), handling semver sorting lot more complex above sort approach can handle.
for example, have sort versions >=version, ~version, ^version, version1 - version2, range1 || range2, , urls, files/paths, github urls, tags, etc.
to handle (valid) versions, it's best use specialized tool, example semver.
Comments
Post a Comment