script to sort pkg names [SOLVED]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

script to sort pkg names [SOLVED]

#1 Post by sc0ttman »

I have a problem, and no solution, as yet.

I want to re-order a list, so that the pkgs are listed in alphabetical order, except that I want the newest version first.

If I use `sort|uniq` I get an alphabetical order, but oldest version first.
If I use `sort -r|uniq` I get an reverse alphabetical order, but newest version first.

How do I get normal alphabetical order, and newest version first?

Code: Select all

# echo "$VAR" | sort
freeciv-1.99
freeciv-2.1
freeciv-2.2
freeciv-3.1
mplayer-1.2.3
mplayer-2.3.4
# echo "$VAR" | sort -r
mplayer-2.3.4
mplayer-1.2.3
freeciv-3.1
freeciv-2.2
freeciv-2.1
freeciv-1.99
EDIT: SOLVED!

This solves it (modified from 3rd post.. added quotes to $VAR.. necessary?):

Code: Select all

echo "$VAR" | sort --field-separator='-' -k1,1d -k2gr
Last edited by sc0ttman on Sun 24 Feb 2013, 10:41, edited 2 times in total.
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#2 Post by GustavoYz »

Deleted...
Last edited by GustavoYz on Thu 07 Feb 2013, 19:59, edited 2 times in total.

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#3 Post by GustavoYz »

Just realize that will fail anyway. :oops:
More like this one:

Code: Select all

echo $VAR | sort --field-separator='-' -k1,1d -k2gr
:P

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

Hi sc0ttman; Did you try -n for numeric sort?
You`ll need to put it in a loop to isolate the package names and then sort each of them numerically.
It works in my tests.

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#5 Post by sc0ttman »

Hi guys..

sunburnt, I know *nothing* about 'sort' and so had tried nowt when posting... I then saw the replies and went and did some experiments... Only using -n, without the -k1 -k2 stuff, worked when stripping out the names entirely, but mixing it all back up again looked daunting!

Again, I know nothing about this, so I might have it wrong.

GustavoYz, that works well, not perfect, but good enough! As far as I can tell, any version numbers that are immediately followed by letters, rather than a dash or underscore etc, do not go where I want, but that's fine... I can live with that.
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#6 Post by GustavoYz »

The thing is that it requires two tipes of sorting, in hierarchical order: alphabetical and then numeric.
[..]As far as I can tell, any version numbers that are immediately followed by letters, rather than a dash or underscore etc, do not go where I want[...]
Yes, if no field separator that could work as a pattern on each filename, it wont work.
I think there is no way if wont split the name in fields somewhere and somehow if it has to be by just using sort, as far as I know.
Hope it works. :P

Post Reply