< Previous | Contents | Next >
Play around a bit with grep, it will be worth the trouble putting some time in this most basic but very powerful filtering command. The exercises at the end of this chapter will help you to get started, see Section 5.5.
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
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
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
567
432
567
34
555
thomas:~> sort itemlist | uniq
1
2
34
4