Sunday, 8 December 2013

How to do Sorting in UNIX???

UNIX sort allows us to sort data in numeric, alphabetic and reverse order whether it’s of column or multiple columns with delimiters. Sort order can be restricted to individual columns and then merged to one single file. Sorting becomes handy in many situations.

Syntax
sort [options] [files...]

Sort Order Commands
Sort -o
Filename to write sorted output to file,
if not given then write to standard output.
Sort –d
Sort in dictionary order
Sort –n
Sort in numeric order(smallest first)
Sort -r
Sort in reverse order (z on top and a on bottom)
Sort –nr
Sort in  reverse numeric order
Sort +1
Sort starting with field 1 (starting from 0)
Sort +0.50
Sort starting with 50th character
Sort +1.5
Sort starting with 5th character of field 1
Sort -c
To see if a file is sorted (error message returned if not)
Sort -u
Suppress duplicate lines and output Identical input lines once.
Sort -M
Sort by month (JAN.FEB, MAR) Note that non-months are
sorted first and uppercase letters precede lowercase
letters for identical months.
Sort -k 1
Sort starting with the first character of the first field
and continuing through the end of the line.

Below we have explained some examples which will make it easy to understand and perform sorting.

1. Simple Sort  .i.e. sort in alphabetical order starting from the first character

Example 1
$ cat sort_number.txt
100
232
3
1
44444
99

$ sort sort_number.txt
1
100
232
3
44444
99

Note: The above records are sorted alphabetically, not mathematically sorted.Same with below

Example 2
$ cat myfile
Saina
Eesto
Jaffer
Michael

$ sort myfile
Eesto
Jaffer
Michael
Saina


2. Sort Numerically 

Example 1
$cat sort_number.txt
100
232
3
1
44444
99

$sort -n sort_number.txt
1
3
99
100
232
44444


3. Sort numerically in Reverse order

Example 1
$sort -nr sort_number.txt
44444
232
100
99
3
1


4. Sort first column numeric, second column alphabetic, third numeric

Example 1
$cat sort_test.txt
1,a100,200,
1,a101,2
1,b100,123
1,b101,6,
1,b101,2
1,b102,2
2,a100,4
1,a100,45
2,b100,23
2,b100,1

$sort -t "," -k1n,1 -k2,2 -k3n,3 sort_test.txt
1,a100,45
1,a100,200,
1,a101,2
1,b100,123
1,b101,2
1,b101,6,
1,b102,2
2,a100,4
2,b100,1
2,b100,23



5. Sort by all three fields in reverse order

Example 1
$ sort -t "," -k1nr,1 -k2r,2 -k3nr,3 sort_test.txt
2,b100,23
2,b100,1
2,a100,4
1,b102,2
1,b101,6,
1,b101,2
1,b100,123
1,a101,2
1,a100,200,
1,a100,45


6. Sort only the third column

Example 1
$ cat sort_test.txt
1,a100,200,
1,a101,2
1,b100,123
1,b101,6,
1,b101,2
1,b102,2
2,a100,4
1,a100,45
2,b100,23
2,b100,1

$ sort -k3,3 sort_test.txt
1,a100,200,
1,a100,45
1,a101,2
1,b100,123
1,b101,2
1,b101,6,
1,b102,2
2,a100,4
2,b100,1
2,b100,23
  

7. Sort file by Name, Year, Month and Day

Example 1
$ cat sort_emp.txt
KELVIN BRAMES     10    JAN 2000
JAMES VINSE       02    MAR 1980
MOHAN NAIR       30    DEC 2012
JAMES VINSE       4     OCT 2011
KELVIN BRAMES     9     FEB 2000
MOHAN NAIR       01    DEC 2012

$ sort -k1,2 -k5,5n -k4,4M -k3,3n sort_emp.txt
JAMES VINSE       02    MAR 1980
JAMES VINSE       4     OCT 2011
KELVIN BRAMES     10    JAN 2000
KELVIN BRAMES     9     FEB 2000
MOHAN NAIR       01    DEC 2012
MOHAN NAIR        30    DEC 2012


8. Sort file with delimiter by Name, Year, Month and Day

Example 1
$ cat sort_emp.txt
KELVIN|BRAMES|10|JAN|2000
JAMES|VINSE|02|MAR|1980
MOHAN|NAIR|30|DEC|2012
JAMES|VINSE|4|OCT|2011
KELVIN|BRAMES|9|FEB|2000
MOHAN|NAIR|01|DEC|2012

$ sort -t"|" -k1,2 -k5,5n -k4,4M -k3,3n sort_emp.txt
JAMES|VINSE|02|MAR|1980
JAMES|VINSE|4|OCT|2011
KELVIN|BRAMES|10|JAN|2000
KELVIN|BRAMES|9|FEB|2000
MOHAN|NAIR|01|DEC|2012
MOHAN|NAIR|30|DEC|2012


 9. Sort the file,  file_sort  in alphabetical order and save the output in the file  Out_file.

Syntax:
$ sort -o Out_file  file_sort


Please share your valuable comments ......

5 comments:

  1. One of the most beneficial aspects of the sort command was left out of the summary; the sort command is compiled C and runs lightening fast. The cost benefit alone justifies using flat-file sorting, as opposed to database sorting.

    Thanks for the blog

    ReplyDelete
  2. Very nice article!
    Thanks
    Partha

    ReplyDelete
  3. very helpful!
    tahnk u a lot!!

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...

ShareThis