Script / sort question; sort on last field of a line?

Walter Dnes waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org
Thu May 4 05:45:32 UTC 2006


  How do I sort on the last (or 2nd last or whatever) field of a line?

  One awkward possibility involves rewriting the lines backwards,
sorting on what is now the first field, and rewriting the lines
backwards a second time.  I'm not aware of a utility to do write lines
backwards.  I am *NOT* thinking of "tac" which writes the lines
unaltered but reverses their order.  I want to write *EACH INDIVIDUAL
LINE* backwards.  The following script does what I want...

#!/bin/bash
while read
do
  line_out=""
  line_pointer=$(( ${#REPLY} - 1 ))
  while [[ ${line_pointer} -ge 0 ]]
  do
    line_out="${line_out}${REPLY:${line_pointer}:1}"
    line_pointer=$(( ${line_pointer} - 1 ))
  done
  echo "${line_out}"
done

-- 
Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org> In linux /sbin/init is Job #1
My musings on technology and security at http://tech_sec.blog.ca
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list