Categories: Unix Tutorials

What are Filter Commands in Unix | Filter commands example

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

Syntax :

Sort [options] [Filename]

Following are some important options of sort file :

  1. -r : This  option is used to sort the elements in descending order.
  2. -n : This option is used to sort the data in numerical order.
  3. -b : Ignores the leading spaces in each line
  4. -d:This option uses dictionary sorting.It only considers spaces and alphanumeric characters.
  5. -m : This option sorts the data by considering the month.The first 3 letters are considered as month.
  6. -f : This option is used for case sensitive sorting of data.
  7. -k: Sorts the data by specified field  position
  8. -u: It suppresses duplicate line
  9. -t: This option works as input field seperator

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

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:

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.

2. Uniq Command :

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.

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:

  1. History of SQL
  2. SQL Create Table(DDL in SQL)
  3. SQL DML Statements(INSERT,UPDATE,DELETE)
  4. SQL Select Statement Execution
  5. Operators in SQL
  6. Views in SQL
  7. Materialized View in SQL
  8. Joins in SQL
  9. Inner Join / Outer Join
  10. Full Outer Join / Cartesian Join
  11. Union and Union ALL
  12. Intersect and Minus
  13. Indexing in SQL
  14. Rank and Dense Rank
  15. SubQueries and Correlated Subqueries
  16. Parser and Optimizer
  17. Oracle 11 G new Features
  18. SQL Functions List
  19. Constraints in SQL
  20. Database Normalization
  21. Table Partitioning
  22. Pivot in SQL
  23. Difference Between Truncate,Delete and drop
  24. Oracle System Tables

Unix Tutorials :

1.What is unix?

2.Basic unix commands

3.File Commands in unix

4.Create File in Unix using multiple ways

5.Cat Command

6.Touch Command

7.Mkdir command

8.rmdir Command

9.pwd command

10.Cd Command

11.cut Command

12.paste Command

13.tr Command

14.Cp Command

15.wc command

16.cmp command

17.Rm Command

18.Grep Command

19.Egrep Command

20.FGrep Command

Hope Everyone likes this article. If you like this article dont forget to comment in comment section.

 

 

 

 

 

 

 

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

4 weeks ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

4 weeks ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago