Sorting Text Files

The sort command is an handy tool to sort text files in linux. Its very simple and easy to use.

Lets say we want to sort the following file with the file name sorttest:
d
Av
R
cx
ts
Sa
v
sf
xv
Xb
3

ds
G

4r
sd
0
92q
0
a
8j
df
9a
kd
ya
da
dfj
0ad

We call sort using the following command:

sort sorttest -o sortresult

It generates the following output:

0
0
0ad
3
4r
8j
92q
9a
a
Av
cx
d
da
df
dfj
ds
G
kd
R
Sa
sd
sf
ts
v
Xb
xv
ya

Notice how the first couple of lines were left blank.

Based on your choice you can pass the right option and use sort to ignore case(-f), ignore leading blanks(-b) ect.

Enhanced by Zemanta