Sort concept confusion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort concept confusion
# 1  
Old 08-29-2014
Sort concept confusion

By default, sort reorders lines in ASCII collating sequence --- whitespace first, then numerals,uppercase letters and finally lowercase letters.

Shellscript:cat sort.txt
Code:
aaa
bbb
ddd
AAA
eee
GGG
ggg

Shellscript:sort sort.txt
Code:
aaa
AAA
bbb
ddd
eee
ggg
GGG

Why the default output does not come out with the small letters first and at the end capital letters.

I was thinking that the output of sort sort.txt should come out as:-
Code:
aaa
bbb
ddd
eee
ggg
AAA
GGG

Thanks

Last edited by Don Cragun; 08-29-2014 at 05:17 AM.. Reason: Add CODE tags for output.
# 2  
Old 08-29-2014
Hi,

The flavour of Unix that you are using has an impact here, some would function as you expect - I think SVR3 behaved in that manner by default.

Regards

Dave
# 3  
Old 08-29-2014
According to the standards, the default sort order matches the order of characters in the ASCII code set. So:
Code:
sort sort.txt

should give you:
Code:
AAA
GGG
aaa
bbb
ddd
eee
ggg

And, the command:
Code:
sort -f sort.txt

(with -f used to fold lowercase characters to uppercase for comparisons) should yield:
Code:
AAA
aaa
bbb
ddd
eee
GGG
ggg

What OS are you using? What is the output from the commands:
Code:
type sort
alias sort

on your system?
# 4  
Old 08-29-2014
The sort order (of the GNU sort command) is determined by the locale you specified in your environment:
Code:
$ export LC_ALL=en_US.utf8
$ sort s.txt
aaa
AAA
bbb
ddd
eee
ggg
GGG
$ export LC_ALL=POSIX
$ sort s.txt
AAA
GGG
aaa
bbb
ddd
eee
ggg


Last edited by cero; 08-29-2014 at 06:03 AM..
These 2 Users Gave Thanks to cero For This Post:
# 5  
Old 08-29-2014
Quote:
Originally Posted by cero
The sort order (of the GNU sort command) is determined by the locale you specified in your environment:
Code:
$ export LC_ALL=en_US.utf8
$ sort s.txt
aaa
AAA
bbb
ddd
eee
ggg
GGG
$ export LC_ALL=POSIX
$ sort s.txt
AAA
GGG
aaa
bbb
ddd
eee
ggg

Thanks for the information. I wasn't aware that the Linux LC_COLLATE category of the en_US.utf8 Locale used a different sort order of ASCII characters than the POSIX and C Locales. On the system I've been working on recently (OS X), as long as all of the characters being sorted were ASCII characters, the C, POSIX, and any English locale (i.e. LC_COLLATE=en_CountryCode.codeset) would all produce identical sort output.
# 6  
Old 08-29-2014
Thank you Cero for the concept. I will google for LC_ALL and LC_COLLATE.
I got my desired output.

shellscript:cat sort.txt
Code:
aaa
bbb
ddd
AAA
eee
GGG
ggg

shellscript:sort sort.txt
Code:
aaa
AAA
bbb
ddd
eee
ggg
GGG

shellscript:
Code:
export LC_ALL=en_US.utf8

shellscript:sort sort.txt
Code:
aaa
AAA
bbb
ddd
eee
ggg
GGG

shellscript:export LC_ALL=POSIX
shellscript:sort sort.txt
Code:
AAA
GGG
aaa
bbb
ddd
eee
ggg

shellscript:
# 7  
Old 08-29-2014
You do not need to export the variable, you can also use it locally, like so:
Code:
LC_ALL=C sort file

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies

2. UNIX for Dummies Questions & Answers

ACL concept

Hi.. Could someone explain about setfacl,getfacl in unix and its uses. Regards, Suresh (1 Reply)
Discussion started by: suresh sunkara
1 Replies

3. UNIX for Advanced & Expert Users

Looping concept please help

Hi Gurus, Please help me in below requirement. Instance =5 (it is user parameter) total=52 (it is user parameter i need to split this to 5 and reminder as 1 instances totally 6 for example i need to splitt to each 52/5=10.4 1-10 11-20 21-30 31-40 41-50 (2 Replies)
Discussion started by: ragu.selvaraj
2 Replies

4. UNIX for Dummies Questions & Answers

help me in RAID concept...

i couldn't get what does the metainit command represents in numeric values. (i.e) #metainit d66 2 1 c0t0d0s4 1 c0t0d0s5 ??here 2 1 1 represnts what ?? can some one tell clearly about this... (6 Replies)
Discussion started by: sriniv666
6 Replies

5. AIX

Use of mirroring concept....

hi.... Friends... Why using mirroring ? what is the use of mirroring? just any one tell about clearly.... thanks.... (4 Replies)
Discussion started by: Kannan841
4 Replies

6. Shell Programming and Scripting

Concept Of Array

Hi all, I used array a lot in C,VB,C# and java but now i am very new to shell programming,so i need a start of array in shell programming. All i want to do is read a string and put it into a character type array. For reading the string,i did this: $ read a now i want to put the content of a... (1 Reply)
Discussion started by: gautamshaw
1 Replies

7. UNIX for Dummies Questions & Answers

about concept of Interrupts.

Hi all, I am new here ,i want to know about interrupts in detail.What r Interrupts .how they r handeled. Thanx in adavnce. (1 Reply)
Discussion started by: vishwasrao
1 Replies

8. UNIX for Dummies Questions & Answers

The Concept of thread

Hi all This is my first thread here.i confused with the concept of thread.Can anyone tell me this concept in detail.my Quation may be at primary level. Thanx in advance for help. (1 Reply)
Discussion started by: vishwasrao
1 Replies

9. HP-UX

LUN concept

In my location we have Hp storage (san),from there they created luns, so could u plz send me clear document for understanding LUN. (1 Reply)
Discussion started by: Prem
1 Replies
Login or Register to Ask a Question