Sorting strings in reverse order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting strings in reverse order
# 1  
Old 10-31-2012
Sorting strings in reverse order

Hello,
I have a large database of words and would like them sorted in reverse order i.e. from the end up.
An example will make this clear:

Quote:
John
Mary
Henry
does
doing
writes
Quote:
doing
does
writes
Dennis
Mary
Henry
I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem to work. I also tried to invoke the scalar reverse function but this does not work.
The data is in an Indic language and is huge: around 300,000 strings.
Any help in the script would be highly appreciated.
# 2  
Old 10-31-2012
How about this :

Quote:
$ cat test
John
Mary
Henry
does
doing
writes

$ rev test |sort |rev
doing
John
does
writes
Mary
Henry
Perl solution :

Quote:
perl -nle 'print scalar reverse $_' filename | sort |perl -nle 'print scalar reverse $_'

Last edited by ningy; 10-31-2012 at 03:09 AM..
This User Gave Thanks to ningy For This Post:
# 3  
Old 10-31-2012
Many thanks will check and get back to you.
# 4  
Old 10-31-2012
With perl (without invoking the interpreter twice and without calling any external utility but memory-intensive depending on the file size):
Code:
perl -lne 'push @strs,$_;END{print for map {scalar reverse} sort map {scalar reverse} @strs}' file


Last edited by elixir_sinari; 10-31-2012 at 09:04 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 10-31-2012
Many thanks. Am out and don't have a perl compiler to check the code, but will definitely get back to you with the results.
Thanks for all your help

---------- Post updated at 06:58 AM ---------- Previous update was at 04:14 AM ----------

Many thanks to both. The two solutions worked beautifully. The one proposed by elixir_sinari is faster and as he himself states
With perl (without invoking the interpreter twice and without calling any external utility):
Thanks for the quick postings and the useful code. I learned finally how to handle scalar reverse, which is a great learning experience for me.
This User Gave Thanks to gimley For This Post:
# 6  
Old 10-31-2012
Hi.

Among the many additional features of utility msort compared to standard sort is the ability to specify a reverse sequence of keys:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate reversed character key sequencing, msort.
# See: http://freecode.com/projects/msort

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, normal key sequencing:"
msort -q --line -n 1,1 $FILE

pl " Results, reversed key sequencing:"
msort -q --line -n 1,1 -R $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
John
Mary
Henry
does
doing
writes

-----
 Results, normal key sequencing:
Henry
John
Mary
does
doing
writes

-----
 Results, reversed key sequencing:
doing
John
does
writes
Mary
Henry

The msort code was in the Debian repository (as well as Fedora, Ubuntu, and FreeBSD). See site mentioned in script for others.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issues with sorting in reverse order

I have a unix script that outputs a summary file to the mac desktop. The file is called summary.txt I am trying to configure such so that the summary.txt file lists the content contained within such in reverse sort order. I have used sort -r but it does not seem to work. I would be... (8 Replies)
Discussion started by: Braveheart
8 Replies

2. Shell Programming and Scripting

print in reverse order

Hi, I want to print the item in reverse order such that the output would look like 00 50 50 23 40 22 02 96 Below is the input: 00 05 05 32 04 22 20 69 Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: reignangel2003
5 Replies

3. Shell Programming and Scripting

How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe) abc|xyz 123|567 i have to get the above in reverse order xyz|abc 567|123 Pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Programming

Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this: 101,99,22HD,432,300HD I have tried using the sort routine like this: sort(mychans.begin(),mychans.end()); For some reason my channels are not being sorted at all. I was hoping someone might have some input that might... (2 Replies)
Discussion started by: sepoto
2 Replies

5. Shell Programming and Scripting

cut a field, but with reverse order

Hi Everyone, I have one a.txt: a b 001 c b b 002 c c c, not 002 c The output should be 001 002 002 If i use cut -f 3 -d' ', this does not work on the 3rd line, so i thought is any way to cut the field counting from the end? or any perl thing can do this?:confused: ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

6. UNIX for Dummies Questions & Answers

sort -reverse order

I need to sort the particular column only in reverse order how i can give it.. if i give the -r option the whole file is getting sorted in reverse order. 1st 2nd col 3rd C col 4th col 5th col ------------------------------------------- C... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

7. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies

8. Shell Programming and Scripting

sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last.. eg. Sample file 1 h a f 8 6 After sort should look like 6 8 f a h 1 (11 Replies)
Discussion started by: frustrated1
11 Replies

9. UNIX for Dummies Questions & Answers

using sed and regex to reverse order???

so i have been trying to learn how to manipulate text on my own and have gotten stumped... let's say i have a text file that says (highly simplified): people ordinary How would swap the order of the words.. I know i need to use sed and some kind of back reference but cannot make it... (2 Replies)
Discussion started by: urtherhoda
2 Replies
Login or Register to Ask a Question