Sorting by a key with offset and length



  • How to sort a file at a certain offset? e.g. my sort key is offset 5 length of 2



  • @Abhay-Gokhale

    Well you can try making a column selection first and then execute Edit (menu) -> Line Operations -> Sort Lines

    Example:

    Imgur

    It may work for you. I’ve found that Notepad++ doesn’t do a stable sort…so YMMV depending upon what you are doing…



  • @Abhay-Gokhale You could always type sort /h in a command shell.



  • Hello, @abhay-gokhale, and All,

    The is a safe way to sort with an offset, in Notepad++. The trick is to copy the key sort, in front of each line to be sorted ;-))

    Let’s start with that sample text ( A long line from the license.txt file, split in several lines )

    The licenses for most software are designed to take away your freedom to share 
    and change it. By contrast, the GNU General Public License is intended to 
    guarantee your freedom to share and change free software--to make sure the 
    software is free for all its users. This General Public License applies to 
    most of the Free Software Foundation's software and to any other program whose 
    authors commit to using it. (Some other Free Software Foundation software is 
    covered by the GNU Library General Public License instead.) You can apply it 
    to your programs, too.
    

    I, simply, inserted a 5 digits number, surrounded by a space character, at column 11 of each line, giving the following text :

    The licens 37529 es for most software are designed to take away your freedom to share 
    and change 12345  it. By contrast, the GNU General Public License is intended to 
    guarantee  94113 your freedom to share and change free software--to make sure the 
    software i 88060 s free for all its users. This General Public License applies to 
    most of th 46931 e Free Software Foundation's software and to any other program whose 
    authors co 88060 mmit to using it. (Some other Free Software Foundation software is 
    covered by 88060  the GNU Library General Public License instead.) You can apply it 
    to your pr 62473 ograms, too.
    

    That five digits number will be the expected key sort. Now, applying the regex S/R :

    SEARCH (?-s)^.{11}(.{5}).+

    REPLACE \1\x20\x20\x20$0

    you should get the text :

    37529   The licens 37529 es for most software are designed to take away your freedom to share 
    12345   and change 12345  it. By contrast, the GNU General Public License is intended to 
    94113   guarantee  94113 your freedom to share and change free software--to make sure the 
    88060   software i 88060 s free for all its users. This General Public License applies to 
    46931   most of th 46931 e Free Software Foundation's software and to any other program whose 
    88060   authors co 88060 mmit to using it. (Some other Free Software Foundation software is 
    88060   covered by 88060  the GNU Library General Public License instead.) You can apply it 
    62473   to your pr 62473 ograms, too.
    

    Then, after running the sort operation ( Edit > Line Operations > Sort Lines Lexicographically Ascending, you get :

    12345   and change 12345  it. By contrast, the GNU General Public License is intended to 
    37529   The licens 37529 es for most software are designed to take away your freedom to share 
    46931   most of th 46931 e Free Software Foundation's software and to any other program whose 
    62473   to your pr 62473 ograms, too.
    88060   authors co 88060 mmit to using it. (Some other Free Software Foundation software is 
    88060   covered by 88060  the GNU Library General Public License instead.) You can apply it 
    88060   software i 88060 s free for all its users. This General Public License applies to 
    94113   guarantee  94113 your freedom to share and change free software--to make sure the 
    

    Notice that, when the key sort is identical ( case of 88060 ), for several lines, the lines are, in addition, sorted, according to the entire line contents ! ( authors - covered - software )

    Finally, using this second simple regex S/R, below, we get rid of the temporary key sort, at column 1 :

    SEARCH (?-s)^.{5}\h+

    REPLACE Leave EMPTY

    and obtain our expected text :

    and change 12345  it. By contrast, the GNU General Public License is intended to 
    The licens 37529 es for most software are designed to take away your freedom to share 
    most of th 46931 e Free Software Foundation's software and to any other program whose 
    to your pr 62473 ograms, too.
    authors co 88060 mmit to using it. (Some other Free Software Foundation software is 
    covered by 88060  the GNU Library General Public License instead.) You can apply it 
    software i 88060 s free for all its users. This General Public License applies to 
    guarantee  94113 your freedom to share and change free software--to make sure the 
    

    Et voilà !

    Best Regards,

    guy038


Log in to reply