简体   繁体   中英

Linux sort combining -V and -f

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.

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 ~]#

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
~ >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM