Find the answer to your Linux question:
Results 1 to 6 of 6
I want to have the sort utility to be case sensitive. What I mean is if I have a file like this: a b C d e and sort it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228

    [SOLVED] How to make 'sort' case sensitive


    I want to have the sort utility to be case sensitive. What I mean is if I have a file like this:

    a
    b
    C
    d
    e

    and sort it the upper-case C should be 1st. The output should be:

    C
    a
    b
    d
    e

    It seems sort is always case insentive (-f option) and I can't find a way to turn it off.

    Thanks.

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    I don't think sort does that. I think it only would work numerically. But you can check for aliases in your system by running alias:

    Code:
    # alias
    alias l.='ls -d .* --color=tty'
    alias ll='ls -l --color=tty'
    alias ls='ls --color=tty'
    alias mv='mv -i'
    alias rm='rm -i'
    alias vi='vim'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    If you want text manipulation, I would suggest Perl.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    Sort should work case-sensitive by default, unless you have an alias for sort that turns on the --ignore-case (-f) option. Most systems auto-alias commands like ls to give more "user friendly" output.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    Ok. I tried this on mine with the input being
    a
    b
    c
    A
    B
    C

    and the output was
    a
    A
    b
    B
    c
    C

    So, I guess you are correct. Seems like a bug to me... However, msort with the proper options seems to work ok. The output for that was
    A
    B
    C
    a
    b
    c

    which is what one would expect.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #5
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    Here is a Perl solution:
    Code:
    #!/usr/bin/perl
    @letters= qw(a b C d e);
    print "Before letters:\n";
    print "@letters\n";
    @sorted_letters =  sort @letters;
    print "After letters:\n";
    print "@sorted_letters\n";

  7. #6
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Quote Originally Posted by Rubberman View Post
    So, I guess you are correct. Seems like a bug to me...
    Nope, it's not a bug it's a feature. ( Where have I heard that before? ) After some more digging around I determined that is the intended behaviour of GNU sort. To get (what I think is) the correct behaviour you have to set LC_ALL to "C".

    Code:
    # sort a.a
    a
    A
    b
    B
    c
    C
    # export LC_ALL=C
    # sort a.a
    A
    B
    C
    a
    b
    c
    Thanks eveyone for your suggestions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •