I have a file consisting of several lines and columns. The columns are delimited by \t. Now I would like to sort the file based on the second column, which can be done with, e.g. sort -f -t$'\t' -k2 file.txt. However, the second column consists of the following values: +, o, and -. How can I sort such that + is on top, o in the middle, and - on the bottom?
Furthermore, I have another column, say column 5, and would like to sort the file w.r.t. that column. However, I want to specify my personal order, so that n is on top, s is in the middle, and l is on the bottom.
If there is a program, which can achieve my goals efficiently, I would prefer that solution. Otherwise I'm looking for a self-written function.
sortutility 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 tryperl -e 'sub k { index "+o-", ($_[0]=~/\t(.)/, $1) } print sort {k($a)<=>k($b) } <>'filesortto use them: I too am interested in the whole recipe ;-)second row consists ..., you meansecond column? Do column 2 and 5 only contain single letter ?+,o,-) and deadline ((n)ow, (s)oon, (l)ater). No other values occur in the mentioned rows.perl -ne 'print sort <>' filewill fetch all the input lines, sort and print them. But perl'ssortallows you to use your own comparator, as insort {k($a)<=>k($b)} <>; in my example, thekfunction returns 0, 1, 2 based on whether the 2nd column starts with-,oor+. For your extran,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