Filter Commands in Unix (Sort Command / Uniq Commannd / Date command) :
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:
1. Sort Command :
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
Sort [options] [Filename]
Following are some important options of sort file :
- -r : This option is used to sort the elements in descending order.
- -n : This option is used to sort the data in numerical order.
- -b : Ignores the leading spaces in each line
- -d:This option uses dictionary sorting.It only considers spaces and alphanumeric characters.
- -m : This option sorts the data by considering the month.The first 3 letters are considered as month.
- -f : This option is used for case sensitive sorting of data.
- -k: Sorts the data by specified field position
- -u: It suppresses duplicate line
- -t: This option works as input field seperator
Creation of file :
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.
$sort [option] Filename
The above statement directly sorts the data by using ASCII values.
Sort the data in descending order :
$sort -r sort_file
The Above statement sorts the data in a file in descending order.
Display only unique lines by eliminating duplicate values :
$sort -u sort_file
The above statement will remove duplicate values and sorts data in ascending order.
1.2. Sorting the Number Data in ascending and descending order :
Using -n option of sort command we can sort the numbers.
Create a file which contains only number:
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.
Type 3 :
Sorting number in descending order :
$ Sort -nr Sort_Number_File
This command sorts the data in descending order.
1.3.Sorting Delimiter File :
Create Delimiter File:
456 | Amit
125 | Ajay
23 | Rahul
You can sort delimiter file.
$Sort -t ‘|’ – nrk Delimiter.txt
23 | Ajay
125 | Amit
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 :
Ctr + D
$Sort -k2 Field.txt
Here -k 2 stands for field 2 and default delimiter is considered as space.
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:
$Sort -m Month.txt
Here by considering the first 3 letters of the month the file data has been sorted.
2. Uniq Command :
It displays unique lines in the given file but the file contents must be in Sorted Order.
Creation of File :
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.
3.Date Commad :
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”
Click on Topic You want to learn:
- History of SQL
- SQL Create Table(DDL in SQL)
- SQL DML Statements(INSERT,UPDATE,DELETE)
- SQL Select Statement Execution
- Operators in SQL
- Views in SQL
- Materialized View in SQL
- Joins in SQL
- Inner Join / Outer Join
- Full Outer Join / Cartesian Join
- Union and Union ALL
- Intersect and Minus
- Indexing in SQL
- Rank and Dense Rank
- SubQueries and Correlated Subqueries
- Parser and Optimizer
- Oracle 11 G new Features
- SQL Functions List
- Constraints in SQL
- Database Normalization
- Table Partitioning
- Pivot in SQL
- Difference Between Truncate,Delete and drop
- Oracle System Tables
Unix Tutorials :
4.Create File in Unix using multiple ways
Hope Everyone likes this article. If you like this article dont forget to comment in comment section.