How to Do 'sort -V' in Osx

How to do `sort -V` in OSX?

Do they all start with a fixed string (char in your example)? If so:

sort -k1.5 -n

-k1.5 means to sort on the first key (there’s only one key in your example) starting from the 5th character, which will be the first digit. -n means to sort numerically. This works on Linux too.

How to sort files in a folder in Mac using Terminal?

Simply use sort which is alphabetical by default:

ls | sort

Using the sort command in BASH in osx 10.9.4

They're sorted in ASCII order. Numbers come before upper case letters, which come before lower case letters. Symbols and punctuation have various places in between and around them. You can try doing LC_COLLATE=en_US sort and see if it gives a better result.

Portable way to achieve ls' -v flag (i.e. sort by version)?

sort -n -t- -k2 seems to do what you want. -n gives you numeric (i.e. not alphabetic) sort; -t- sets the field separator to -, and -k2 selects the second field, i.e. the version number.

My sort, on Ubuntu, even does the part with the underscore correctly, but I'm not sure if that is standard. To be sure, you could sort by the minor version first, then by major version.

bash sort alphanumeric buildnumber

I assume your real full list won't have all b versions. You'll need to split field 4 into two keys; one for the alpha part and one for the numeric part.

$: sort -t. -k1n -k2n -k3n -k4.1,4.1 -k4.2n vnums
1.0.0.a5
1.0.0.a10
1.0.0.a13
1.0.0.a19
1.0.0.b1
1.0.0.b6
1.0.0.b8
1.0.0.b9
1.0.0.b12
1.0.0.b14
1.0.0.b17
1.0.0.b20
1.0.0.b21
1.0.0.b22
1.0.0.c3
1.0.0.c7
1.0.0.c15
1.0.0.c16
1.0.0.d2
1.0.0.d4
1.0.0.d11
1.0.0.d18
1.0.3.b66
1.1.3.b5

Note the limiting of the alpha column of field 4 to a single character.

strange uniq output on Mac

From the uniq man page:

Repeated lines in the input will not be detected if they are not
adjacent, so it may be necessary to sort the files first.

macbook:stackoverflow joeyoung$ cat c.txt
a
g
b
b
g
g
c
v
c
macbook:stackoverflow joeyoung$ uniq c.txt
a
g
b
g
c
v
c
macbook:stackoverflow joeyoung$ sort -u c.txt
a
b
c
g
v
macbook:stackoverflow joeyoung$ sort c.txt | uniq
a
b
c
g
v


Related Topics



Leave a reply



Submit