In previous article i have explained the basic filter commands like Cut,Paste,Translate commands.In This article i will explain another set of filter commands which are used to filter the data from the files.This article gives idea about the different commands in unix like Sort Command with example. I will explain the basic options which are useful in day to day activities.I will explain following commands with real life examples in detail:
Sort Command in unix is basically used to order the element in the file.Sort command sorts the elements in the file by its ascii values.Sort command sorts numarical values as well as it sorts the string n to the file.Sort command basically order the lines in text file.Using following asciii values the sort command will work :
0-9 Numbers : 48 to 57 Ascii values
A-Z letters : 65 to 90 Ascii values
a-z small letters : 97 to 122 Ascii values
Syntax :
Sort [options] [Filename]
Following are some important options of sort file :
Creation of file :
$ cat>Sort_File
a
z
a
A
1
Ctrl+d
1.1. Sort Data in Ascending and Descending order :
We can directly use the Sort command to sort the data in ascending or descending order. But To sort the data in descending order.
Syntax :
$sort [option] Filename
Example :
$sort sort_file
Output :
1
A
a
a
z
The above statement directly sorts the data by using ASCII values.
Sort the data in descending order :
Example :
$sort -r sort_file
The Above statement sorts the data in a file in descending order.
Output :
z
a
a
A
1
Display only unique lines by eliminating duplicate values :
Example :
$sort -u sort_file
The above statement will remove duplicate values and sorts data in ascending order.
Output :
1
A
a
z
Using -n option of sort command we can sort the numbers.
Create a file which contains only number:
cat>Sort_Number_File
456
125
23
Type 1 :
As per Ascii:
$ Sort Sort_Number_File
It sorts above file as per ascii value.
Type 2 :
Using -n option:
$ Sort -n Sort_Number_File
It sorts the data by considering the numbers.
Output :
23
125
456
Type 3 :
Sorting number in descending order :
$ Sort -nr Sort_Number_File
This command sorts the data in descending order.
Output :
456
125
23
1.3.Sorting Delimiter File :
Create Delimiter File:
cat>Delimeter.txt
456 | Amit
125 | Ajay
23 | Rahul
You can sort delimiter file.
$Sort -t ‘|’ – nrk Delimiter.txt
Output :
23 | Ajay
125 | Amit
456| Rahul
1.4. Sorting based on field position :
User can sort the data in the file based on field position of the file.There is -k option to handle this situation.The sort command uses space and Tab as default Delimiter.
Creation of file :
cat>Field.txt
456 Amit
125 Ajay
23 Rahul
Ctr + D
Example :
$Sort -k2 Field.txt
Here -k 2 stands for field 2 and default delimiter is considered as space.
Output :
456 Ajay
125 Amit
23 Rahul
In above example only field 2 has sorted.
1.5. Sorting by month:
You can sort the data by month also.The month should be considered as first 3 letters of any month. e.g. January= jan
Creation of file:
cat>Month.txt
October
February
January
Example :
$Sort -m Month.txt
Output :
January
February
October
Here by considering the first 3 letters of the month the file data has been sorted.
It displays unique lines in the given file but the file contents must be in Sorted Order.
Creation of File :
$ cat>Unique_File
aaa
aaa
ccc
ddd
hhh
hhh
hhh
ppp
Ctrl+d
Example :
1) $ uniqUnique_File
2) $ uniq –u Unique_File
It displays Non-Duplicate lines
3) $ uniq –d Unique_File
It displays only duplicated lines
4) $ uniq –c Unique_File
It counts how many times the lines are repeated in the file.
Date command is used to dispay the date of the system.
Following are some important option of date command :
%m Month of the year (in digits)
%d Day of month (in digits)
%y Year (last two digits)
%D Date as mm/ dd/yy
%H Hour (OO to 23)
%M Minutes (OO to 59)
% T Time as HH:MM:SS
$ date “+%T”
Unix Tutorials :
4.Create File in Unix using multiple ways
12.paste Command
Hope Everyone likes this article. If you like this article dont forget to comment in comment section.
Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…
In my previous article I have given Top 20 technical support interview questions with its…
In my previous articles I have given 15 most asked desktop support interview questions with…
A business analyst is someone who is versed in processes of data analysis used for…
In my previous article I have already given top questions and answers for Desktop support…
In my previous article I have given unix production support interview questions and answers. In…