How can I sort a list of words
apple
banana
orange
healthy
by the reverse of the words, the result should be
banana
orange
apple
healthy
CodePudding user response:
Use the rev
utility to reverse lines characterwise. Then rev
again.
$ cat input.txt | rev | sort | rev
banana
orange
apple
healthy
Edit 1
As pointed out by @dan, the better solution is to do rev input.txt | sort | rev
.
CodePudding user response:
Mostly just because it's always nice to have a chance to recall perl
:
perl -nE 'push @a, scalar reverse $_ } { print scalar reverse $_ foreach sort @a' input-file
or
perl -nE 'push @a, $_ } { print foreach (sort { (scalar reverse $a) cmp (scalar reverse $b) } @a) ' input-file
or
perl -e 'my @a = <>; print foreach (sort { (scalar reverse $a) cmp (scalar reverse $b) } @a)'