1. Computing & Technology

Introduction to Linux

From Machtelt Garrels

5.3.2. Filtering output

The command sort arranges lines in alphabetical order by default:


   

 
thomas:~> cat people-I-like | sort
 Auntie Emmy
 Boyfriend
 Dad
 Grandma
 Mum
 My boss
 

But there are many more things sort can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files first, biggest files last:

ls -la | sort -nk 5


       Old sort syntax
        

You might obtain the same result with ls -la | sort +4n , but this is an old form which does not comply with the current standards.

The sort command is also used in combination with the uniq program (or sort -u ) to sort output and filter out double entries:


   

 
thomas:~> cat itemlist
 1
 4
 2
 5
 34
 567
 432
 567
 34
 555
 
 thomas:~> sort itemlist | uniq
 1
 2
 34
 4
 432
 5
 555
 567
 


   Prev    Home    Next
   Advanced redirection features    Up    Summary

* License

* Introduction to Linux Index

©2012 About.com. All rights reserved.

A part of The New York Times Company.