Sorting a device list

Michaël Grünewald Michael.Grunewald at laposte.net
Tue Dec 1 14:57:22 UTC 2009


Peter Steele wrote:

>Can anyone recommend a quick and dirty way to sort a device list? For example, if I do this:
>I need to skip the device prefix before applying the -g option. Something like this works:
>
>ls /dev/ad*|sort -g -k 1.8
>
>/dev/ad4
>/dev/ad6
>/dev/ad8
>/dev/ad10
>
>but this assumes the device name is just two characters long. I want a quick way to sort a generic device list like this, considering only the numeric part of the device for the key. Is there a quick and dirty way to do this or do I need to pipe it into a perl script or something?
>
You can use sed to insert a sepcial character before the first digit, 
use sort with this special character as field delimiter, and then remove 
the special character with another call to sed. The following pipeline 
does it:

sed -e 's/\([0-9]\)/@\1/' | sort -t @ -n -k 2 | sed -e 's/@//'

(This assumes `@' does not appear in the names of the devices you are 
working with.)

Hope this helps!
-- 
Michaël



More information about the freebsd-questions mailing list