A possible solution in Perl:<br><br>----------------------------------<br>#!/usr/bin/perl -w<br><br># Sort using the last word of each line.<br><br>use strict;<br>use warnings;<br><br>my %hash;<br><br>while(<>) {<br>
<br> # Strip any newline, split line into words.<br><br> chomp;<br> my @a=split(/ /,$_);<br><br> # Stuff the line into a hash of arrays, using the last word as the hash key.<br><br> push(@{$hash{$a[-1]}},$_);<br>}<br>
<br># Dump out the sorted (using the default sorting algorithim) results from the<br># hash.<br><br>foreach my $key ( sort keys %hash ) {<br><br> # Within each key there may be several lines; dump them out in the order
<br> # that they were encountered.<br><br> foreach(@{$hash{$key}}) {<br><br> print "$_\n";<br> }<br>}<br><br># Done.<br>----------------------------------<br><br>To sort on the second to last field, change the '-1' to '-2'; you can also subsitute your own sort algorithim after the word 'sort'. You can also change what consitutes a 'word' by changing the delimiter in the split statement.
<br><br>-- <br>Alex Beamish<br>Toronto, Ontario<br><br><div><span class="gmail_quote">On 5/4/06, <b class="gmail_sendername">Walter Dnes</b> <<a href="mailto:waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org">waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org</a>> wrote:</span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> How do I sort on the last (or 2nd last or whatever) field of a line?<br><br> One awkward possibility involves rewriting the lines backwards,
<br>sorting on what is now the first field, and rewriting the lines<br>backwards a second time. I'm not aware of a utility to do write lines<br>backwards. I am *NOT* thinking of "tac" which writes the lines<br>
unaltered but reverses their order. I want to write *EACH INDIVIDUAL<br>LINE* backwards. The following script does what I want...<br><br>#!/bin/bash<br>while read<br>do<br> line_out=""<br> line_pointer=$(( ${#REPLY} - 1 ))
<br> while [[ ${line_pointer} -ge 0 ]]<br> do<br> line_out="${line_out}${REPLY:${line_pointer}:1}"<br> line_pointer=$(( ${line_pointer} - 1 ))<br> done<br> echo "${line_out}"<br>done<br><br>--
<br>Walter Dnes <<a href="mailto:waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org">waltdnes@waltdnes.org</a>> In linux /sbin/init is Job #1<br>My musings on technology and security at <a href="http://tech_sec.blog.ca">http://tech_sec.blog.ca</a>
<br>--<br>The Toronto Linux Users Group. Meetings: <a href="http://tlug.ss.org">http://tlug.ss.org</a><br>TLUG requests: Linux topics, No HTML, wrap text below 80 columns<br>How to UNSUBSCRIBE: <a href="http://tlug.ss.org/subscribe.shtml">
http://tlug.ss.org/subscribe.shtml</a><br></blockquote></div><br><br>