Linux sort combining -V and -f

217 views Asked by At

I want to combine sort -V and -f. Is there a way?

Here is simple example. I want to sort this list.

> cat testme

a1
a2
a11
a12
a3
A8
B8
b1
b11

Default sort is upper case first, lower case second, plus a11 comes before a2

> cat testme | sort

A8
B8
a1
a11
a12
a2
a3
b1
b11

I use -V which is awesome, a2 is before a11, but its still upper case then lower case

> cat testme | sort -V

A8
B8
a1
a2
a3
a11
a12
b1
b11

I can sort -f which fixes case, but a11 is still before a2

>cat testme | sort -f

a1
a11
a12
a2
a3
A8
b1
b11
B8

I try and combine them but -V wins and -f loses.

>cat testme | sort -f -V

A8
B8
a1
a2
a3
a11
a12
b1
b11

Is there an option to combine these?

Desired output is:

a1
a2
a3
A8
a11
a12
b1
B8
b11

Version in use:

[03:11:09] sa-hq1:~ # sort --version
sort (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
2

There are 2 answers

6
Nehal Dattani On

On my Fedora 25, it works properly.

    [root@localhost ~]# sort --version
    sort (GNU coreutils) 8.25
    Copyright (C) 2016 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Written by Mike Haertel and Paul Eggert.
    [root@localhost ~]# sort -Vf testme
    a1
    a2
    a3
    A8
    a11
    a12
    b1
    B8
    b11

[root@localhost ~]#
0
Shakiba Moshiri On

You no need to cat file to sort command. It can read a file from stdin or from argument list:

sort -Vf file

or

sort -Vf < file  

test:

~ > sort -Vf < file

a1
a2
a3
A8
a11
a12
b1
B8
b11
~ > sort -Vf file

a1
a2
a3
A8
a11
a12
b1
B8
b11
~ >