Sort alphabetically starting from specified letter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort alphabetically starting from specified letter
# 1  
Old 10-21-2015
Sort alphabetically starting from specified letter

Hi.

I'm trying to sort a list of items in a file alphabetically but starting from the letter 'X'. For instance if I had the following file;

test.txt

Code:
Z
A
T
W
Y
B
S
X

I would like the output to look like;

Code:
X
Y
Z
A
B
S
T
W

I've looked through all the documentation and have experimented with the sort command (sort -r etc) , but cant find anything that can do the above.

Can sort do this?
Thanks
# 2  
Old 10-21-2015
Nope. The reason why this is non-trivial is that the letter set for any alphabet is variable and this is interesting because "good" solutions to this would have to know the letter set.

The solution, should you attempt it, could be to create an artificial mapping table to create an invisible sort field (one that is temporal) that maps X = A, Y = B, Z = C, A = D, and so on... the mapping would vary depending on what "letter" (in your example you chose X) you wanted to start with. I'm assuming a 26 char standard english alphabet.

I imagine somebody here will write a simple one in awk (or something) and then you can sort on the temporaral field and remove the field in post processing. Or better, you can write this.
This User Gave Thanks to cjcox For This Post:
# 3  
Old 10-21-2015
What if you broke the file into two (fileAW and fileXZ) , based on a <="X" for the first field.
Sort fileAW to fileAWs
Sort fileXZ to fileXZs
then concatenate the files, combining fileXZs and fileAWs
# 4  
Old 10-21-2015
An awk approach:-
Code:
awk -v S="X" '
        BEGIN {
                n = split( "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", A )
                for ( i = 1; i <= n; i++ )
                {
                        if ( A[i] == S )
                                s_idx = i
                }
        }
        {
                R[$1]
                next
        }
        END {
                if ( s_idx > 1 )
                {
                        for ( j = s_idx; j <= n; j++ )
                        {
                                if ( A[j] in R )
                                        print A[j]
                        }
                        for ( k = 1; k < s_idx; k++ )
                        {
                                if ( A[k] in R )
                                        print A[k]
                        }
                }
                else
                {
                        for ( j = 1; j <= n; j++ )
                        {
                                if ( A[j] in R )
                                        print A[j]
                        }
                }
        }
' filename

This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-21-2015
Hi.

Utility msort allows alternate sequences:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate alternate collating sequence, msort.
# For msort, see your local repository or,
# http://www.billposer.org/Software/msort.html
# Verified: Wed Oct 21 11:02:51 CDT 2015

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
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 column

FILE=${1-data1}

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

pl " Sort order file (columnized):"
column custom-1.txt

pl " Results:"
msort -q -j -l -n 1,1 -s "custom-1.txt" $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 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
msort 8.44
column - ( /usr/bin/column, 2007-11-20 )

-----
 Input data file data1:
Z
A
T
W
Y
B
S
X

-----
 Sort order file (columnized):
X	A	D	G	J	M	P	S	V
Y	B	E	H	K	N	Q	T	W
Z	C	F	I	L	O	R	U

-----
 Results:
X
Y
Z
A
B
S
T
W

I have found msort in Debian, Ubuntu, Fedora, MiNT, OSX (port), etc. Otherwise see the link in the script comments.

Best wishes ... cheers, drl
# 6  
Old 10-21-2015
How about
Code:
sort file |  sed -n '/X/,${p;d}; wTMP' | cat - TMP && rm TMP
X
Y
Z
A
B
S
T
W

This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-21-2015
Code:
sort file | sed -n '
${p;x;}
/X/,${p;d;}
1h;1!H
'


Last edited by MadeInGermany; 10-21-2015 at 03:48 PM.. Reason: optimized
This User Gave Thanks to MadeInGermany For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to sort a file alphabetically

I have a problem with my homework I need to create a shell script using #!bin/awk -f the script will output the file in an alphabetical order only words and after the word is : after that a space then , then it will be numbered each character by which line its been for example CB 92A A... (1 Reply)
Discussion started by: collapse
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

3. Shell Programming and Scripting

Get all File names starting with letter P

Hi, I have lets say 10 files , I need to process them one by one. So I need a command to get one file name at a time to process it into a variable Example Files P1111.dat P3344.dat S344.dat ... v_file_name = 'p111.dat' .. I will rename it to something after processing ... (1 Reply)
Discussion started by: prassu
1 Replies

4. Homework & Coursework Questions

Grep for filetype starting with letter p

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Which files in /usr/bin whose names begin with “p” are python scripts? Store the numbered results in... (3 Replies)
Discussion started by: alindner
3 Replies

5. Shell Programming and Scripting

Sort alphabetically, then numerically

Greetings - I'm not necessarily new to bash scripting - I'm probably between beginner and intermediate, but I have something that I just cannot figure out after many attempts to find it. I have a file that is merely a list of many files, with their respective paths, and a branch path (ClearCase)... (5 Replies)
Discussion started by: 1cor29
5 Replies

6. Shell Programming and Scripting

Sort by numbers, then alphabetically

Hey guys, I have a file that contains the following: 366 K 364 Q 12 UB 7 INC. P 4 Law 2 LAMB 2 High 1 QEG 1 OF 1 LC 1 B As you can see, it's already sorted by numerical order, how do I sort it again, breaking the ties by using the alphabetical order of the second column, but... (2 Replies)
Discussion started by: Andrew9191
2 Replies

7. UNIX for Dummies Questions & Answers

sort lines in different files based on the starting letter

Hi ,, i have the below file... D 2342135 B 214236 C argjlksd V lskjrghaklsr C slkrgj B sdg4tsd E aslkgjlkasg i want to sort the lines into different files based on the starting letter of the line. so that i have different files for lines starting with a letter. thanks (1 Reply)
Discussion started by: jathin12
1 Replies

8. UNIX for Dummies Questions & Answers

Sort file alphabetically AND numerically

Hi all. I have 2 files like this: f1 A 10 B 80 C 9 f2 A 11 B 700 C 10 What I want is the concatenation of the two files sorted by name (alphabetically) and size (numerically), so the result should be like this: F3 (cat f1 f2 sorted) A 10 A 11 B 80 B 700 (2 Replies)
Discussion started by: mrodrig
2 Replies

9. UNIX for Dummies Questions & Answers

How to sort alphabetically after finding values

I have a list of people in a usage log and need to print the names and phone numbers of people with over 500 logins. I'd also like to display these names alphabetically. I have their total logins set to a variable named total. So far, I have very little in my awk script to do this: FS=":"... (4 Replies)
Discussion started by: doubleminus
4 Replies

10. Shell Programming and Scripting

Sum of Files Sizes starting with a letter...

Can we find some of size of all files in a directry where file names start with an letter t* the out put of ls -ls t* is 4 -rw-r--r-- 1 root system 61 Jul 03 10:56 t 4 -rw-r--r-- 1 root system 3146 Jul 19 11:11 t1 4 -rw-r--r-- 1 root system ... (2 Replies)
Discussion started by: pbsrinivas
2 Replies
Login or Register to Ask a Question