Sorting a list of words one per line by their ending


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a list of words one per line by their ending
# 1  
Old 10-07-2013
Sorting a list of words one per line by their ending

Hello,
My OS is Windows and therefore DOS. Hence I have no access to Unix tools.
I am trying to sort a file in Urdu by the character by which it ends. Each word is on a separate line.
As input, an example in English would help:
Code:
fruit
banana
apple
pear
house

I need the sort to be on the last letter of the word and then sorted recursively in reverse order
Code:
banana
apple
house
pear
fruit

A script in PERL or AWK would do just great.
Just in case someone wants to work with live data, here is a sample:
Code:
ببا
ببجا
ببچو
ببد
ببر
ببڑ
ببز
ببستگيا
ببلا
ببلو
ببلے
ببلی
ببند
ببنسلکلگے
ببنسلکلئ
ببهنو
ببہتر
ببھا
ببھر
ببھی
ببو
ببيا
ببيشتر
ببے
ببی
بپا
بپتا
بپتسما
بپد
بپر
بپفسر
بپلی
بپھر
بپو
بتا
بتبا
بتتر
بتتے
بتتی
بتجد
بتجو
بتحقی
بتخے
بتد
بتذ
بتر
بتز
بتستا
بتصحی
بتصر
بتطا
بتعثنا

Many thanks
# 2  
Old 10-07-2013
Quote:
Originally Posted by gimley
Hence I have no access to Unix tools.
[...]
A script in PERL or AWK would do just great.
Isn't that somewhat contradictory? As it is, awk is a UNIX tool, no?

If you have awk you probably have sort too (or can get it). I do not know Urdu, so please forgive me if i ask: is Urdu written backwards (right to left)? If so, you might want to know that LC_ALL and all the other internationalization variables influence the operation of sort. Might it be that, if you properly set these variables to the correct language, the sorting is already done from the other side?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 10-07-2013
I agree with bakunin, let the locale do all the work if possible.
Just in case the OP means something different, a Perl code that sorts
by the last letter:
Code:
perl -e'
  print map $_->[0],
    sort {
      $a->[1] cmp $b->[1]
     }
      map {
       [ $_, substr $_, -2, 1 ]
        } <>' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 10-07-2013
Many thanks for your advice and the perl code. Off the record
Code:
AWK

is the only tool I know which has been configured for Windows.
Code:
SED

works but to a limit, unfortunately.
Urdu is written right to left. How do I set LC_ALL for sorting? Any solutions ?
The PERL script works wonderfully for English /Latin data but does not work for Urdu ?
Many thanks once more
# 5  
Old 10-07-2013
No short answer, sorry. Check this thread. I suppose you'll need to try with the ur-PK locale.
Edit: It seems that ur-PK is not currently available for Unicode::Collate::Local ...
# 6  
Old 10-07-2013
Try busybox and then
Code:
rev file | sort | rev
banana
apple
house
pear
fruit

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

3. UNIX for Beginners Questions & Answers

Search for words starting and ending with

im trying to search for a WORD in a file which begins with a number followed by a hypen follwed multiple words and end with a dot "." and pront the entire line which matches the above. Please note that there is a space at the begining of each line i/p file 19458 00000-CONTROL-PARA.... (5 Replies)
Discussion started by: anijan
5 Replies

4. Shell Programming and Scripting

Add text at start and ending of every line

Hi all, Is there other way to Add text at start and ending of every line? here my script: cat file.txt |awk '{print "<p align=\"justify\">"$0"</p>"}' but the problem they put including white spaces, I only need those line have a sentence or text not an skip all have empty string or have... (7 Replies)
Discussion started by: lxdorney
7 Replies

5. UNIX for Dummies Questions & Answers

Sorting files ending in numbers with "sort"

I have a group of files that I need to be sorted by number. I have tried to use the sort command without any luck. ls includes* includes1 includes10 includes11 includes12 includes2 includes3 includes4 includes5 includes6 includes7 includes8 includes9 I have tried ls includes*... (6 Replies)
Discussion started by: newbie2010
6 Replies

6. UNIX for Dummies Questions & Answers

Sorting words based on length

i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Discussion started by: yairpg
1 Replies

7. UNIX for Dummies Questions & Answers

Deleting words and sorting

I have a file that looks some like this: I need to delete most of the information and sort the rest in such way that I get the following output file Any help will be greatly appreciated (3 Replies)
Discussion started by: Xterra
3 Replies

8. Shell Programming and Scripting

Help in retrieving the ending line numbers of the functions

Hi! I've a C file which consist of many function definitions with numbers at the beginning as shown below.. 10 void search() 11 { 12 /*body 14 * 15 * 17 * 18 * 40 * 42 * 60 } 90 void func_name() 95 { 99 /*body 100 * 105 * 111 * (7 Replies)
Discussion started by: abk07
7 Replies

9. UNIX for Advanced & Expert Users

help with sorting sequence in Unix C:sort -t ':' +0 -1 -n +1 -2 +2 -3 -o list list

Hi List is 000|2008-07-17|556543|RTJ|35-RTGJ|EYT 465|2008-11-10|567789|GHJ|45-DGHH|ETU 533|2008-09-06|567789|GHJ|45-DGHH|ETU How does it do it? sort -t ':' +0 -1 -n +1 -2 +2 -3 -o list list (6 Replies)
Discussion started by: gurvinder
6 Replies

10. Shell Programming and Scripting

Appending line ending with '}" to new line

Hello masters. I have a rather simple problem but its been killing me. I have a file "x" with only 1 line inside it. The line looks something like Now this is only part of the line. Its actually about 4000 characters. What i need to do is whenever there is a "}", i need to append the next... (4 Replies)
Discussion started by: aismann
4 Replies
Login or Register to Ask a Question