Basic Shell Commands

Go to Problems

Sort and Uniq command in Unix with example

Sort

Input:

     1	denationalise
     2 gingal
     3 bladdery
     4 asepticism
     5 cringing
     6 chalutzim
     7 fanner
     8 jubbulpore
     9 eidolon
    10 overhasty
Sorting alphabetically ( Flag -k used to select a field for sorting)
cat words.txt | sort -k 2 

Output:

     4	asepticism
     3 bladdery
     6 chalutzim
     5 cringing
     1 denationalise
     9 eidolon
     7 fanner
     2 gingal
     8 jubbulpore
    10 overhasty
Uniq

Filter out repeated lines in a file.

Input:

1
2
2
3
3
3
4
4
4
4
5
5
5
5
5
Select unique lines
cat nums.txt | uniq

Output:

1
2
3
4
5

Select unique lines along with their count using -c (count) flag
cat nums.txt | uniq -c

Output:

   1 1
   2 2
   3 3
   4 4
   5 5

Serious about Learning Scripting ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum.
Shell scripting
Problem Score Companies Time Status
Valid phone number 150
22:24
Lines in a given range 200
27:49
Remove Punctuations 250
12:58
Transform CSV 300
19:07