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.

Required fields*

7
  • How big is your file? AFAIK the sort utility doesn't support plugging in your own comparator, and there are no other readily available tools capable of sorting huge files. If loading the whole file in the memory is OK, then try perl -e 'sub k { index "+o-", ($_[0]=~/\t(.)/, $1) } print sort {k($a)<=>k($b) } <>' file Commented Jan 19, 2020 at 18:37
  • Of course, you can probably define your own locales & collation rules, and get sort to use them: I too am interested in the whole recipe ;-) Commented Jan 19, 2020 at 18:43
  • When you say second row consists ..., you mean second column ? Do column 2 and 5 only contain single letter ? Commented Jan 19, 2020 at 18:46
  • It's a small file consisting of no more than 100 lines whereby each line represents a task. Tasks have a priority (+, o, -) and deadline ((n)ow, (s)oon, (l)ater). No other values occur in the mentioned rows. Commented Jan 19, 2020 at 18:49
  • 1
    That would take a whole perl tutorial, but: just perl -ne 'print sort <>' file will fetch all the input lines, sort and print them. But perl's sort allows you to use your own comparator, as in sort {k($a)<=>k($b)} <>; in my example, the k function returns 0, 1, 2 based on whether the 2nd column starts with -, o or +. For your extra n, s, l, you can change it to: perl -e 'sub k { $_[0] =~ s/.*?\t//r =~ y/-o+nsl/abcabc/r } print sort {k($a) cmp k($b)} <>' file Commented Jan 19, 2020 at 19:20