Hmm, and if you want to reverse the entire line (read the spec, read the spec, read the spec), just push 'reverse $_' inside the first loop. <br><br><div><span class="gmail_quote">On 5/4/06, <b class="gmail_sendername">Alex Beamish
</b> <<a href="mailto:talexb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">talexb-Re5JQEeQqe8AvxtiuMwx3w@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;"><div style="direction: ltr;">
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></div><div style="direction: ltr;"><span class="sg"><br>-- <br>Alex Beamish<br>Toronto, Ontario</span></div><div style="direction: ltr;"><span class="e" id="q_10b00f273811474b_2"><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" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">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" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">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" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://tech_sec.blog.ca</a>
<br>--<br>The Toronto Linux Users Group. Meetings: <a href="http://tlug.ss.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">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" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://tlug.ss.org/subscribe.shtml</a><br></blockquote></div><br><br>
</span></div></blockquote></div><br><br clear="all"><br>-- <br>Alex Beamish<br>Toronto, Ontario<br>