Skip to content

sort: full -k KEYDEF parsing with start/end fields, character positions, and per-key flags #906

@chaliy

Description

@chaliy

Problem

sort -k currently only parses a single field number (e.g. -k2 or -k2n). The GNU coreutils key specification is much richer and scripts in the wild rely on it.

Missing features

  • End-field specs: -k2,3 (sort by fields 2 through 3)
  • Character positions: -k2.3,3.4 (start at field 2 char 3, end at field 3 char 4)
  • Per-key flags: -k2n,2 vs -k3r,3 (different sort modes per key)
  • Multiple keys: -k1,1 -k2n (primary sort by field 1 lexically, secondary by field 2 numerically)

Current code

sortuniq.rs:137-145 — parses -k value by extracting leading digits and checking for n/r suffix. No comma handling, no multi-key support.

Suggested approach

uutils/coreutils (MIT) has a robust key parser in their sort implementation that handles the full GNU syntax. Consider borrowing or adapting the parsing logic. The comparison/sorting itself can stay as-is since it already works against bashkit's VFS.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions