0

So, I have a JSON array:

[ "3.22-SNAPSHOT_293022", "3.22-SNAPSHOT_296087",
"3.22-SNAPSHOT_latest", "3.22.1_293094", "3.22.1_296087",
"3.22.1_latest",, "3.23-SNAPSHOT_308024", "3.23-SNAPSHOT_308310", "3.23-SNAPSHOT_latest", "3.23.1_307802", "3.23.1_308022",
"3.23.1_latest", "4.0-SNAPSHOT_307842", "4.0-SNAPSHOT_307938",
"4.0-SNAPSHOT_308031", "4.0-SNAPSHOT_308193",
"4.0-SNAPSHOT_308308", "4.0-SNAPSHOT_308310", "latest" ]

First number indicates the version no. 2nd number indicates sub version number. I want to de-delect the items such that, a. 2 highest version numbers (which is not a snapshot) have 2 sub version left b. 2 highest version numbers (snapshot) have 2 sub version left c. rest versions have 1 subversion left.

Other entries can be deleted. Any point for the ways to do it would be great..

Pranay
  • 498
  • 1
  • 4
  • 16
  • 1
    I advice using a JSON aware tool under bash. http://stackoverflow.com/questions/38364261/parse-json-to-array-in-shell-script – Doqnach May 17 '17 at 16:30
  • Invalid JSON. There's empty element after '3.22.1_latest'. – Nikhil Vartak May 17 '17 at 16:33
  • Using a regular expression (by itself) to select the 'highest' of anything is the wrong tool. Is there any reason you have to use bash for this? Python or Ruby or just about anything else would be better: Read the elements from the array, extract the number with a regex, and do whatever you want with it. – alan May 17 '17 at 16:35
  • Expected output should be this: [ "3.22-SNAPSHOT_296087", "4.0-SNAPSHOT_307842", "4.0-SNAPSHOT_307938", "4.0-SNAPSHOT_308031", "4.0-SNAPSHOT_308193", ] – Pranay May 17 '17 at 16:35

1 Answers1

1

Ways to do it:

  • Use bash regex combined with the -V (--version-sort) option of sort(1) and the jq utility to deal with JSON.
  • Use Python with the json module and the LooseVersion() function from distutils.version

I would opt for the latter... I'll leave the fun up to you until you request help with some code that you've tried.

Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31