Shell Programming Question - sort

Edward Vielmetti emv at math.lsa.umich.edu
Sat Mar 3 20:02:29 AEST 1990


In article <751 at ncs.dnd.ca> marwood at ncs.dnd.ca (Gordon Marwood) writes:

   I am trying to sort (using "sort" in Ultrix) based on the last two
   characters in a line (which are numeric).  There are a variable number
   of characters in a line, and these last two characters are preceded by a
   space, there are also a variable number of spaces in a line, so the
   number of fields will be variable if space is used as the field
   separator.  None of my available texts gives me a clue as to whether a
   sort can be done based on the last field in a line, regardless of the
   number of fields in the line.  Is there any "sort" option that can 
   do this ?

Here's how I'd do it in perl:

perl -e '@all=<>;sub l {substr($a,2,-2)-substr($b,2,-2);}; print sort l @all;'

If I had to do it without perl, I'd use awk to copy the last two 
characters on the line to the beginning, sort on those, and then
sed them out.  Glad I don't have to do that anymore.

Just ano...no, you don't want to hear that,

--Ed

Edward Vielmetti, U of Michigan math dept.



More information about the Comp.unix.questions mailing list