• Sort from second field instead on second field

    From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed Mar 29 17:53:16 2023
    From Newsgroup: gnu.emacs.help

    You can sort lines in the region on the second field with:
    C-u 2 M-x sort-field

    In most cases this will probably be enough. But what if I would want
    to sort from the second field to the end of the line?
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From HASM@hasm@example.invalid to gnu.emacs.help on Wed Mar 29 09:39:43 2023
    From Newsgroup: gnu.emacs.help


    You can sort lines in the region on the second field with:
    C-u 2 M-x sort-field
    In most cases this will probably be enough. But what if I would want
    to sort from the second field to the end of the line?

    Silly recipe:

    - mark at the first char of the first line of the region

    - go to last char of the last line of the region

    - M-x untabify

    - add enough spaces to the first line of the region to make it as long
    as the longest line in the region

    - go to end of the first line, and mark

    - go to the beginning of the second field on the last line

    - M-x sort-columns

    Replace region with full buffer, if that's what you need
    First and last line can be reversed above
    Use C-u M-x sort-columns to reverse the sort order, as usual

    -- HASM
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to gnu.emacs.help on Wed Mar 29 19:28:17 2023
    From Newsgroup: gnu.emacs.help

    * Cecil Westerhof <Cecil@decebal.nl>
    | You can sort lines in the region on the second field with:
    | C-u 2 M-x sort-field

    | In most cases this will probably be enough. But what if I would want
    | to sort from the second field to the end of the line?

    Mark the region and call

    C-u M-x shell-command-on-region RET sort -k FIELDNUM RET

    -k, --key=KEYDEF
    sort via a key; KEYDEF gives location and type

    KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where
    F is a field number and C a character position in the field; both are
    origin 1, and the stop position defaults to the line's end. [...]

    ? Of course requires an external command compatible with Unix' 'sort(1)'.

    R'
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed Mar 29 19:55:37 2023
    From Newsgroup: gnu.emacs.help

    HASM <hasm@example.invalid> writes:

    You can sort lines in the region on the second field with:
    C-u 2 M-x sort-field
    In most cases this will probably be enough. But what if I would want
    to sort from the second field to the end of the line?

    Silly recipe:

    - mark at the first char of the first line of the region

    - go to last char of the last line of the region

    - M-x untabify

    - add enough spaces to the first line of the region to make it as long
    as the longest line in the region

    - go to end of the first line, and mark

    - go to the beginning of the second field on the last line

    - M-x sort-columns

    Replace region with full buffer, if that's what you need
    First and last line can be reversed above
    Use C-u M-x sort-columns to reverse the sort order, as usual

    Works. Thanks.
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Thu Mar 30 07:03:03 2023
    From Newsgroup: gnu.emacs.help

    Ralf Fassel <ralfixx@gmx.de> writes:

    * Cecil Westerhof <Cecil@decebal.nl>
    | You can sort lines in the region on the second field with:
    | C-u 2 M-x sort-field

    | In most cases this will probably be enough. But what if I would want
    | to sort from the second field to the end of the line?

    Mark the region and call

    C-u M-x shell-command-on-region RET sort -k FIELDNUM RET

    -k, --key=KEYDEF
    sort via a key; KEYDEF gives location and type

    KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where
    F is a field number and C a character position in the field; both are
    origin 1, and the stop position defaults to the line's end. [...]

    ? Of course requires an external command compatible with Unix' 'sort(1)'.

    For me that would work because I am on Debian. I was thinking about
    this originally, but then I saw that Emacs had his own internal
    commands. So I thought that is better. It can be done in the way HASM
    said. (And it is really neat.) But I think that this is a better, or
    at least easier way. So next time I will use the external sort
    command.
    Thanks.


    I have:
    Efo# Mobile Phone
    Efy! Loudly Crying Face
    Ef-f Window
    EfAi House with Garden
    EfAo Broken Heart
    EfAa House
    EfN| Partying Face
    EfN| Hot Face

    And when using (I prefer long options):
    C-u M-x shell-command-on-region RET sort --key=2 RET

    I get:
    EfAo Broken Heart
    EfN| Hot Face
    EfAa House
    EfAi House with Garden
    Efy! Loudly Crying Face
    Efo# Mobile Phone
    EfN| Partying Face
    Ef-f Window
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2