Can you sort one column lexicographically while simultaneously sorting the next column numerically?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).



I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.



Edit: Accidentally typed column in place of row.










share|improve this question























  • Possible duplicate of Trying to sort on two fields, second then first
    – Alexander
    Mar 31 '17 at 6:59














up vote
3
down vote

favorite
1












I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).



I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.



Edit: Accidentally typed column in place of row.










share|improve this question























  • Possible duplicate of Trying to sort on two fields, second then first
    – Alexander
    Mar 31 '17 at 6:59












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).



I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.



Edit: Accidentally typed column in place of row.










share|improve this question















I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).



I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.



Edit: Accidentally typed column in place of row.







sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:34









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Mar 31 '17 at 6:53









Anj

183




183











  • Possible duplicate of Trying to sort on two fields, second then first
    – Alexander
    Mar 31 '17 at 6:59
















  • Possible duplicate of Trying to sort on two fields, second then first
    – Alexander
    Mar 31 '17 at 6:59















Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59




Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59










1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:



$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f354995%2fcan-you-sort-one-column-lexicographically-while-simultaneously-sorting-the-next%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:



    $ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
    a 2
    a 13
    b 2





    share|improve this answer
























      up vote
      6
      down vote



      accepted










      Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:



      $ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
      a 2
      a 13
      b 2





      share|improve this answer






















        up vote
        6
        down vote



        accepted







        up vote
        6
        down vote



        accepted






        Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:



        $ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
        a 2
        a 13
        b 2





        share|improve this answer












        Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:



        $ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
        a 2
        a 13
        b 2






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 31 '17 at 7:00









        Stephen Kitt

        158k23344421




        158k23344421



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f354995%2fcan-you-sort-one-column-lexicographically-while-simultaneously-sorting-the-next%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)