Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • Ok. Thanks! I'll try this. And thanks for the comments! Commented Mar 16, 2011 at 11:04
  • 5
    cat -n acts much like awk '{print NR" "$0}' Commented Mar 16, 2011 at 11:09
  • 2
    I think that's called Schwartzian transform, or decorate-sort-undecorate Commented Mar 17, 2011 at 23:25
  • This general approach is nice in that sort handles using files on disk if the task is too big to reasonably do in memory. It might be gentler on memory though if you used temporary files between the steps rather than pipes. Commented Aug 10, 2016 at 6:15
  • 1
    awk -v OFS='\t' '{print NR, $0}' file | sort -k1,1nr | cut -f2- is IMHO the cleanest way to write that as it's still using mandatory POSIX tools but is using awks OFS instead of hard-coding a separator char in the print and doing string concatenation, is using awk to generate input that uses the default separator for cut, \t, is using cut for it's sole purpose instead of making sed do what cut exists to do, and is only sorting on the one, necessary field that contains the line number. Commented Oct 1, 2021 at 21:01