Shell Programming Question - sort
the specific heat
justin at reed.bitnet
Sat Mar 10 09:02:35 AEST 1990
Gordon Marwood says:
>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 ?
The way to do this using standard unix tools which comes most quickly to my
mind is:
$ awk '{ print $NF " " $0}' filename | sort -n | sed 's/[^ ]* //'
. The first command prepends the last field to the line, and the last undoes
that. The output will be on stdout, so you'll have to deal with that. Brad
Appleton suggests using reverse(1STAT) which has, on our machine, a -f (reverse
by fields) option. So you'd do
$ reverse -f <filename | sort -n | reverse -f
. I tried this and got output with the words separated by tabs where they were
originally separated by spaces. Larry Wall suggests, as always, the perl
solution:
$ perl -ne 'chop; print reverse "\n", split(//);'
. I couldn't get this to work, no matter where i put the filename. I may have
a defective version of perl. The chief advantage of the first solution is that
it uses well-documented tools which will be present on any unix box you'll ever
use.
--
JUSTIN at REED.BITNET (or) [tektronix,ogicse]!reed!justin
Member HASA, [/bin/]sed (sOCIETY OF emacs dEVOTEES), Church of the SubGenius
(in BOB we trust), ROCOCO, the Illuminati and any other absurdities i can get
my grubby paws on (suggestions appreciated)
More information about the Comp.unix.questions
mailing list