sort (Sorting file contents)


This command sorts the file contents alphabetically in ascending order by default taking the first character of each line from file. This displays sorted file to standard output.


Syntax:

sort [option] filename


Example:

$cat language
C|primary|4|3
C++|primary|3|3
JAVA|secondary|2|2
SQL|secondary|3|2
UNIX|primary|4|4
$
$sort language
C++|primary|3|3
C|primary|4|3
JAVA|secondary|2|2
SQL|secondary|3|2
UNIX|primary|4|4
$








Options to use:


$cat language (Original File)
C|primary|4|3
C++|primary|3|3
JAVA|secondary|2|2
SQL|secondary|3|2
UNIX|primary|4|4


  • -t" "  - used for delimited can say separator sign, inside " " specify the delimiter symbol


  • -k    - used for specifying column number just after one space


[ Field-wise / Column-wise sorting:
  Syntax: sort -t"delimiter" -k column filename ]


$sort -t"|" -k 2 language
C++|primary|3|3
C|primary|4|3
UNIX|primary|4|4
JAVA|secondary|2|2
SQL|secondary|3|2
$


  • -n   - sorting numbers


  • -r    - sorting in reverse order 


  • -i    - ignore case, consider only printable characters


  • -b   - ignore leading blank space


  • -u   - display unique values, removes duplicates



Examples:


$sort -t"|" -n -k 3 language
JAVA|secondary|2|2
C++|primary|3|3
SQL|secondary|3|2
C|primary|4|3
UNIX|primary|4|4

$
$sort -t"|" -n -r -k 3 language
UNIX|primary|4|4
C|primary|4|3
SQL|secondary|3|2
C++|primary|3|3
JAVA|secondary|2|2
$


We are taking just another file with duplicates and spaces:


$cat language2nd
C++|primary|3|3
C++|primary|3|3
   C|primary|4|3

JAVA|secondary|2|2
 UNIX|primary|4|4
 SQL|secondary|3|2

$
$sort language2nd


   C|primary|4|3
 SQL|secondary|3|2
 UNIX|primary|4|4
C++|primary|3|3
C++|primary|3|3
JAVA|secondary|2|2

$
$sort -b language2nd


C++|primary|3|3
C++|primary|3|3
   C|primary|4|3
JAVA|secondary|2|2
 SQL|secondary|3|2
 UNIX|primary|4|4

$
$sort -b -u language2nd

C++|primary|3|3
   C|primary|4|3
JAVA|secondary|2|2
 SQL|secondary|3|2
 UNIX|primary|4|4
$






Sort command Examples with pipe "|" connector:


$cat language2nd | sort


   C|primary|4|3
 SQL|secondary|3|2
 UNIX|primary|4|4
C++|primary|3|3
C++|primary|3|3
JAVA|secondary|2|2
$
$cat language2nd | sort | uniq

   C|primary|4|3
 SQL|secondary|3|2
 UNIX|primary|4|4
C++|primary|3|3
JAVA|secondary|2|2

$
$cat language2nd | sort -u

   C|primary|4|3
 SQL|secondary|3|2
 UNIX|primary|4|4
C++|primary|3|3
JAVA|secondary|2|2
$


Counting the duplicate lines in a file:


$sort language2nd | uniq -c
   2
   1    C|primary|4|3
   1  SQL|secondary|3|2
   1  UNIX|primary|4|4
   2 C++|primary|3|3
   1 JAVA|secondary|2|2
$


Displaying the duplicate lines in a file:


$sort language2nd | uniq -d

C++|primary|3|3
$



Questions & Answers:


Qs: Without using redirection operator, how to save all data into a new file?


sort -o file1 file2

Contents of file2 will be redirected to file 1.


Qs: How to count the duplicate lines in a file?


sort filename | uniq -c


Qs: How to remove duplicate line from a file?


sort -u filename

sort filename | uniq


Qs: How to display only duplicate lines in a file ?


sort filename | uniq -d



NEXT ->


Cracking IT Interview